找回密码
 注册
查看: 161|回复: 3

TypeError at /hello/world/ 'set' object is not reversible

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-4-18 21:50:10 | 显示全部楼层 |阅读模式
TypeError at /hello/world/'set' object is not reversible
; ], J" E6 W" hTypeError at /hello/world/'set' object is not reversibleRequest Method:        GETRequest URL:        http://192.168.7.26:5000/hello/world/Django Version:        3.0.5Exception Type:        TypeErrorException Value:        'set' object is not reversibleException Location:        C:\Users\admin\python3.8vitual\lib\site-packages\django\urls\resolvers.py in _populate, line 455Python Executable:        C:\Users\admin\python3.8vitual\Scripts\python.exePython Version:        3.8.8Python Path:        ['C:\\Users\\admin\\Desktop\\myproject', 'C:\\Users\\admin\\Desktop\\myproject', 'C:\\Program Files\\JetBrains\\PyCharm ' '2019.3\\plugins\\python\\helpers\\pycharm_display', 'C:\\Program Files\\Python38\\python38.zip', 'C:\\Program Files\\Python38\\DLLs', 'C:\\Program Files\\Python38\\lib', 'C:\\Program Files\\Python38', 'C:\\Users\\admin\\python3.8vitual', 'C:\\Users\\admin\\python3.8vitual\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm ' '2019.3\\plugins\\python\\helpers\\pycharm_matplotlib_backend']Server time:        Tue, 18 Mar 2025 13:44:02 +0000
! B; ]) f! V" ?" s8 b* `+ S
在python控制台上调试错误如下:Internal Server Error: /hello/world/Traceback (most recent call last):  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner    response = get_response(request)  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response    response = self.process_exception_by_middleware(e, request)  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response    response = wrapped_callback(request, *callback_args, **callback_kwargs)  File "C:\Users\admin\Desktop\myproject\hello\views.py", line 9, in hello_world    print(reverse(hello_world))  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\urls\base.py", line 87, in reverse    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix    self._populate()  File "C:\Users\admin\python3.8vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate    for url_pattern in reversed(self.url_patterns):TypeError: 'set' object is not reversible[18/Mar/2025 21:43:50] "GET /hello/world/ HTTP/1.1" 500 81009C:\Users\admin\Desktop\myproject\hello\views.py changed, reloading.Watching for file changes with StatReloaderPerforming system checks...
0 r, R5 V1 d( [% P: t
5 g0 D3 V# \5 l5 R0 ~5 L) m% d
3 u- K0 w- ?! R4 \4 \
python调试代码出错;
3 q7 X7 y: ~6 Z5 b

) K- L2 {6 @  T9 x7 ^
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')
! J: f% @0 e7 M% U
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。
0 g- i* `9 i' Q8 c$ i! A$ ?
怀疑自己的配置有问题,各种测试好像找不到问题所在。
7 Q6 a# z* A5 ]  S# o  \  t3 M* V9 Q& R
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world')  k) ?( K! P' o" d5 r/ L1 G# w
再次访问,既然不报错了。正常反馈结构。
+ }- K; x+ i# S# O+ y6 s$ a2 B8 `
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11
0 n+ c9 o  V# s" l% M
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?6 `2 P+ N5 [3 Y! H. q# {% F
: n) V$ _. t' A/ Z
9 [! U7 J! J0 `/ F, G8 v: _$ M

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面
- t* m+ B) H& N- U8 I2 h  Bdef hello_world(request):
7 `+ H7 J/ [* S# e5 a. U    print('reverse(hello_world)')
, y; E/ ]* C5 ~    return HttpResponse('hello world')
& L6 a9 @7 _+ Q4 i6 _  L7 Z4 G
- C4 K4 X! W% z$ A+ h+ I, `; @
def hello_world(request):+ j% k. o) Z  o, D" e% r9 z8 g
    'reverse(hello_world)'
1 q/ w9 @4 C  P9 t    return HttpResponse('hello world')
* t, e8 \+ p' h
* u: F+ n8 i& S; J: P
) k/ A# R" Q1 H2 L5 r
7 d# V& h; |! e0 ?# b% n5 M[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 11% C5 b1 u7 S/ ]' _# y0 m3 Q+ ^
5 ]9 Y- M& A- G+ n$ X: D
+ w2 |2 X0 e: J1 N; p! I
reverse(hello_world)) {# }& t+ Z- {" p5 B7 U4 E

, I$ i- h' E+ I6 k4 g+ s5 p
' s( {+ U6 f& W
6 u0 q" C( V- u& D9 v3 |5 `) R或者这样子写也行:2 ^9 _  U8 i. C! X
def hello_world(request):) F' g7 h" W3 E1 z
    reverse1 = 'reverse(hello_world)'
: ?, y8 W$ P* C$ H8 M8 B2 c! |    print(reverse1)7 c8 u7 b4 e7 V! O
    return HttpResponse('hello world')" L" p0 X: S) Y0 n. O

$ R3 N$ I% d3 b. M! }* ~4 V5 K3 b都可以获取到逆向解析,并且成功。
4 Z, \5 I; D7 l3 m2 a; y! A7 P- j
% K: `% [3 E! ~5 ^5 \9 R. D

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:21:13 | 显示全部楼层
说明我们上面没有配置成功。出现问题了。只是打印处理reverse(hello world) .

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-23 22:35:38 | 显示全部楼层
Internal Server Error: /hello/china/
' C0 K$ _& @. sTraceback (most recent call last):
- r: y: m+ J/ w- u  H/ Z/ ], v  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner' _  A) E. p& X0 r; ?, n) }
    response = get_response(request). ^9 J2 ^( i  O5 ?5 X
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
. v% z3 ?2 N& U# w! C" B    response = self.process_exception_by_middleware(e, request)
; ]5 `# G& j0 N: P2 x1 C. O! `  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response8 q9 I4 y) j0 g: L. Y3 h: r
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
. Y$ @. F: W! I0 v. {/ I  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china$ W2 v$ y& r7 v4 S( }4 r
    url= reverse('hello_china')
3 I% u. P) Y5 a( M% B+ M  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse
* U5 L7 v) i; I    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))0 d$ N% l4 W6 S1 ~2 b1 w
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix1 I" b& _: O7 t+ i+ Q
    self._populate()
  p5 J* _. y6 ?& M+ s6 r  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate
1 j, j' O4 c4 ?" V' P    for url_pattern in reversed(self.url_patterns):6 V" S; A1 `' A0 S
TypeError: 'set' object is not reversible
1 E8 \2 J9 G# k+ b[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834# ?; ?" S3 r0 L1 k

: W% I4 Q- G8 E  P3 p, V3 c3 i# n7 e: _' f
问题的原因找到了,是因为根路径下的urls.py 文件中
9 h2 T) I. k. V' L! A: L  [! G: N+ i. J0 C1 v6 V9 l( g" A
urlpatterns = {
. n3 _% F: Z+ ?- I* J  ]* S; T" A& D9 W    path('admin/', admin.site.urls),
" G' W9 z, g# |! s: a: n3 P    path('hello/', hello_world),
7 d( X: B+ r1 T    path('hello/', include('hello.urls')),
: Y  Y1 J, Y2 D! }) \8 k3 C}
0 r* n/ T5 H2 f6 K需要改成6 z; P3 w) l0 l' C' O
urlpatterns = [7 ^1 Q, t5 i0 Y# K! G
    path('admin/', admin.site.urls),4 o. Q4 v4 K. {4 U0 m
    path('hello/', hello_world),
, ?6 J8 }! q, P, g# v    path('hello/', include('hello.urls')),$ W* z6 g7 _% j" r: \7 Z
], l, e- M3 }' `
问题完美解决。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:26 , Processed in 0.015695 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表