易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 153|回复: 3
收起左侧

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

[复制链接]
发表于 2025-4-18 21:50:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
TypeError at /hello/world/'set' object is not reversible. k+ }. N- }9 ]7 j2 u# w$ `
TypeError 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
: `7 x1 N7 P# N, H9 R7 \
在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...
  h+ G& \/ b9 [$ n4 Z
, j: n7 I1 }" W; v% g
( h+ x4 a. X$ X7 S8 L+ [
python调试代码出错;
2 ]5 k- D, z: {' N, ~" U3 o

$ s6 @' [* s( _+ `4 \( t+ Z
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')
6 ^: R6 U' y6 {- }5 F* K
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。2 g- d0 Z' A# o
怀疑自己的配置有问题,各种测试好像找不到问题所在。; q+ g5 F+ z5 K8 a5 i3 J
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world')
! s* ^' h/ {+ }( x2 j
再次访问,既然不报错了。正常反馈结构。
' O+ a2 [. D0 h$ _" j! Z
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11& z" I6 i3 O  N+ X. c2 m4 t( A" j
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?
1 a) _* O; y7 F4 T2 j# \& ~- x5 }

/ D( T+ \" v: T- H, F
- D4 j( ]% B, T& M
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面
$ ^- g/ H% T0 W# a  V, q+ Zdef hello_world(request):$ x* F7 [3 {, w
    print('reverse(hello_world)')$ e' r6 T6 {" M( B; E* C$ j: o+ O
    return HttpResponse('hello world')# J0 y( d! d6 Z/ ], A9 _

% l  n1 t" K' y
# `0 c. Y0 R: O4 i- Ldef hello_world(request):
& V4 c  J" F3 ?+ a( |/ C8 M, r    'reverse(hello_world)'+ w, L+ @: [  ~1 P
    return HttpResponse('hello world')4 c  C( I# A, m2 X  y- ^

% U5 N2 G0 E, A$ V  w$ h- _& \. ~- A8 S3 _+ W

/ F4 P1 Y9 p" o9 I; H$ b6 |[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 11
  X  {  x) }, Y$ y, q0 }8 }5 I
4 I2 m; v1 v4 r, \( _) {" _* e; S' P- _' S
reverse(hello_world)% `( m8 Z! P3 z( j) `

( |0 X5 s# ?  ^/ y# n. h0 J! R
& c! c9 o, z3 D/ t# k$ P) }1 s# @5 m2 L" h  d. u) W5 \7 H- F
或者这样子写也行:% u5 S0 T7 {; o& `, b
def hello_world(request):! }0 z* Y- Z7 x3 c" o/ Q
    reverse1 = 'reverse(hello_world)'7 m8 {  w7 c2 E) ?, L
    print(reverse1)6 j5 U+ M$ [7 E+ P$ w  ?
    return HttpResponse('hello world')6 s: R. p% h  j

) u# x1 Q" G3 x, e, q* O都可以获取到逆向解析,并且成功。4 P# `$ J. Z! |* u' L8 X8 O
$ M* i# s; h! J5 }& Z
 楼主| 发表于 2025-4-19 08:21:13 | 显示全部楼层
说明我们上面没有配置成功。出现问题了。只是打印处理reverse(hello world) .
 楼主| 发表于 2025-4-23 22:35:38 | 显示全部楼层
Internal Server Error: /hello/china/  |8 w1 p& {& o
Traceback (most recent call last):
9 _' M# h$ B" D+ {( d  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner7 D4 ~* ?1 A( u5 Z$ i
    response = get_response(request)
0 p6 S5 n& c% g+ ?5 S- e0 }  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response/ a- w: O) e9 K' Q' Z( j: ]$ N$ q
    response = self.process_exception_by_middleware(e, request)/ B8 m. P8 m6 L9 d, d  G( D
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response* m# ^5 j+ S# k& d* W8 ?+ i
    response = wrapped_callback(request, *callback_args, **callback_kwargs); ~% j: n5 T) f* k0 \3 o# n0 H  t
  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china; e# q# _7 Z) F
    url= reverse('hello_china')8 a0 k- z# `* U/ B* m* c/ V
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse% J/ P6 _2 X4 e& i. U7 m6 f) S
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
+ K# C! ]0 @7 _1 y0 I0 K  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix% v: @+ N$ b* s
    self._populate()& u! P/ {( ]. H- n) Q4 w0 y7 F3 k
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate6 E( ]% y0 W& y
    for url_pattern in reversed(self.url_patterns):
, P* I5 c0 P3 J# t3 MTypeError: 'set' object is not reversible; U- l) k9 B/ J& [, Q3 s
[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834( v9 u8 J1 R" H; l" J3 T" X

' y. Z$ }3 j+ P/ P+ O
+ ~0 F$ R* X- d/ F  ]7 v问题的原因找到了,是因为根路径下的urls.py 文件中: ~% H$ t! y4 U( W
! g# e* l3 V0 Y- ~
urlpatterns = {
' f3 |, p0 F3 b% s) p    path('admin/', admin.site.urls),9 U" J- O) n/ n  n$ v
    path('hello/', hello_world),
/ s! P/ j) ], V9 s5 V5 L- Q9 g4 w, N- X    path('hello/', include('hello.urls')),
) ~& ~+ @. ]4 O0 a}
! x- T. m# V: t/ T% _+ ~需要改成
, _) M! s' y& U( m+ b8 Iurlpatterns = [
- i8 `8 T8 S3 T: I* q    path('admin/', admin.site.urls),
5 D4 T4 r& p$ D' W, Q9 a; p6 I+ U    path('hello/', hello_world),
  M, B) k- q$ v. r' m$ _$ X6 N/ t    path('hello/', include('hello.urls')),
1 O1 f! M0 |0 x3 L* k% n]) ^# J6 U; H: c$ N& k4 H
问题完美解决。
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-8 20:23 , Processed in 0.051371 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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