找回密码
 注册
查看: 158|回复: 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  A, k- \: n+ x% Q$ m( @* L% X  S4 {4 y
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
( z6 }, A8 C1 }) {$ l) ?0 W
在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...
' V5 k, ]2 c& S2 K) ^" b6 E3 K% Z9 x6 g% |

$ P* P5 |$ _( Z, xpython调试代码出错;
* v3 g0 s& h& t* q3 Z

0 j# ]- n$ Z) z' k7 r7 q
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')
% [) |3 K2 m( z* J* ~- F
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。% O% W' a6 {# N7 J" V1 g
怀疑自己的配置有问题,各种测试好像找不到问题所在。
7 f( m6 G/ ]5 `- ^' |
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world')1 C* }$ T8 m* X9 H' ?$ }. c" J/ E
再次访问,既然不报错了。正常反馈结构。
: t" ^& y2 `$ @0 q5 f
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11' j; Z% j" v7 E0 z
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?. W8 K+ f! `3 E: Q2 p# M
; y  W% |2 ]5 K" x
- D/ U4 B( H* r% m

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面
1 t5 e/ }, B2 }, U4 Ndef hello_world(request):
) S  I+ l% K( L* D9 q    print('reverse(hello_world)')& ]* w6 N! M3 j; b* O6 S+ A1 Q
    return HttpResponse('hello world')
( F( L, q) j- ?& N
( o8 ~/ F& W' W" }/ R
% h  O4 o1 T! h  @$ L4 |* sdef hello_world(request):' v0 F: E; }& e) \' Z9 {" j# n
    'reverse(hello_world)'' e8 m  m  _8 Q( K% \7 U0 i
    return HttpResponse('hello world')
9 N0 i$ T, i) H- l2 p4 C7 H8 B2 H' _3 a! g; }- r+ S7 s
: x& ]* ?' S% C9 z- y
5 B* c; k8 v4 |& i  R4 U/ b& q' \
[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 112 Y* }2 O1 Y8 u: z" e+ |

0 M) z, }3 p- I. U- A+ z2 j0 }8 W9 \3 L$ Y  T
reverse(hello_world)3 N6 t! I7 {8 S6 q) p
) c) q4 T( H7 `0 E) A6 N

7 }& j) Y! G* j4 W- H& ~( W# a$ u! _# y3 g
或者这样子写也行:/ C; Q6 E# c  A( a& d# p  e( S
def hello_world(request):8 {( @) j# h2 F5 P
    reverse1 = 'reverse(hello_world)'. _. i0 b5 ?& B7 i! R
    print(reverse1)/ G* d8 X9 F, O8 H6 D! z% }6 L
    return HttpResponse('hello world')
. m9 J$ k% P; k7 b- T3 _; z  E* [  P. v% H. Y
都可以获取到逆向解析,并且成功。) P! X6 V2 H# A) s

% O# }5 a1 m8 f2 f" m& l; A

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/& q; H( v9 |  @" x. }
Traceback (most recent call last):4 F- x) L; f" R/ ^, E& @
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
" n5 o7 u( o# M1 w4 R& w* a    response = get_response(request)3 d8 `9 \1 d/ v' I5 q
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response' _2 C! w: ~' s- f! @: D2 R* [
    response = self.process_exception_by_middleware(e, request)+ _3 i, n) m& G5 `
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response4 h$ F' c7 ?. E; T. g, d
    response = wrapped_callback(request, *callback_args, **callback_kwargs)) \3 {" d  Q$ u" M& q
  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china
# q2 w' J4 X, n! I! b    url= reverse('hello_china')% u( S+ \0 F% [9 {# Z! d
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse2 l/ T% n! e+ b; R. A) ?2 y
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))  `, C# ^8 s6 e4 n* P  w
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix
% `$ b* K7 i; g4 x: a    self._populate()4 d- Z& A' b7 ?* c! m  ]
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate; H" W4 J  k' p( a) ~2 t
    for url_pattern in reversed(self.url_patterns):
. K9 y9 o! S8 x& a  E4 yTypeError: 'set' object is not reversible
" K. ~9 c; O% f2 @[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834! Y: t1 g1 I; ~* f) Z

& u$ V1 o( ]" r8 V0 F' c' O: ?. f- Q: b: B- m" _8 s
问题的原因找到了,是因为根路径下的urls.py 文件中
( J: W# h2 ]. q4 s, M- W: `/ U( g0 ]
urlpatterns = {0 S4 n  q. h1 M/ A* X- L
    path('admin/', admin.site.urls),
1 S/ G: j+ F- ^    path('hello/', hello_world),
8 W* G7 f* f7 W, L    path('hello/', include('hello.urls')),; Q& |3 v+ `4 d
}
0 t( ~- z2 m8 \  U# i3 p需要改成- g8 E' G# L1 s8 H' \; X+ m9 f
urlpatterns = [2 o: n) [' ~0 ^' g1 O" D
    path('admin/', admin.site.urls),
5 i& r& X* e. o5 m- {' F: S    path('hello/', hello_world),1 |1 q, t7 ~( f- M; Y2 z2 O8 g7 r
    path('hello/', include('hello.urls')),
/ q7 o' B4 F7 j* E) f! D]
5 g4 T' [, x" `* a问题完美解决。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-11 23:15 , Processed in 0.020369 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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