易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 152|回复: 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
4 ?0 r2 Q* O: N1 C, v! \) bTypeError 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
; o; s9 ^, h7 u- x2 T0 E
在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..." X5 t/ {, H% j; ~
$ E+ |5 c5 Z. {9 S
$ E  P4 F! i) ]' \
python调试代码出错;
4 X: f' m6 T/ b& ]! T4 O  {6 |
+ f: m+ S5 E2 w# q5 ]8 \
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')+ k) O& T$ r! D2 ~1 a" V, j! v5 A
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。( p- E  m# A7 J, v
怀疑自己的配置有问题,各种测试好像找不到问题所在。6 s& J" @( k/ [, j
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world')( C: N! l( _' r# \
再次访问,既然不报错了。正常反馈结构。
* T7 A5 e  w6 C& e: W
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11$ E, `% A. \7 n
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?
! F! P# s& V4 _0 s+ p9 r/ k

9 A$ R* h6 X, l0 c- i- k& L4 e
" Z+ v; i5 K+ k& y* ?. D
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面
. X0 n/ K4 F$ r7 o! K3 Udef hello_world(request):
$ m+ K2 x- _0 p+ H* Q    print('reverse(hello_world)')
, k9 {2 e8 Z% t' ?* O    return HttpResponse('hello world')
5 j2 v4 l$ q% _5 W
5 U1 V2 o. j. \0 a$ x- i& ^8 J0 a! x% {$ f& B
def hello_world(request):4 ~. @  Y- B4 R7 `4 e3 u
    'reverse(hello_world)'& f& O! R  U- O5 c; i+ W0 `5 ?
    return HttpResponse('hello world')3 p9 T$ Z& q5 C) p
8 P/ X- i3 B% I  ^$ n
( A# x$ l9 ~. |9 t' U

' u5 L1 o! w- a% e[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 118 q0 q" T# q9 F1 R  g* q( x1 h, ^

4 r" H! q7 H  B& ~0 P1 S+ @% {% R5 b) t; {
reverse(hello_world)" n4 Q3 |6 O7 t6 @

* [- Z; t9 u+ s% L7 v% ^5 M; c$ w' ^1 K& F/ g8 a

  M/ `$ y3 x6 r5 u8 i' O2 O或者这样子写也行:
* j* C! V6 S0 [, ^% d; m, Idef hello_world(request):" ^/ p& K# c$ w: u+ Z
    reverse1 = 'reverse(hello_world)'
7 z5 X5 x: z. G. y  ?; Q# a9 I* @    print(reverse1)
" S9 _& s! a* n8 X/ t" V+ c% ]    return HttpResponse('hello world')
6 U4 T+ k$ g3 n2 ?1 t" o* J& o4 y" F8 y0 O$ ^" @
都可以获取到逆向解析,并且成功。3 |+ Z0 N) L! C' b* q" N2 l* ~
# v. b7 H( v  z3 g
 楼主| 发表于 2025-4-19 08:21:13 | 显示全部楼层
说明我们上面没有配置成功。出现问题了。只是打印处理reverse(hello world) .
 楼主| 发表于 2025-4-23 22:35:38 | 显示全部楼层
Internal Server Error: /hello/china/
9 i5 x7 K' t! }$ ^7 x6 vTraceback (most recent call last):
) E! A% k" y& L/ m: h$ ~0 k" W0 v  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
" i9 b( |: w) O' N5 N/ T9 {    response = get_response(request), Q' x: r" l4 l3 q' C
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response/ i, {4 [8 i# B, ~! b
    response = self.process_exception_by_middleware(e, request)* u, U; e9 ?; m( I
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
: I" h: n! X. |  c5 S3 W5 G    response = wrapped_callback(request, *callback_args, **callback_kwargs)) c3 j7 @- ]3 T1 T. \
  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china8 p- d. ^1 Y4 @- \0 @
    url= reverse('hello_china')
( L3 H! P9 A, Q1 ~/ ]: b, K1 i  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse
$ b9 P* D& c5 m7 v1 A- ?    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
( S: o6 x3 F) K4 f/ f4 E  g7 O& c  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix
2 \5 I( M: k1 j8 G# V: w    self._populate()% P( ?5 i! D" I; r5 B% \
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate
4 f$ v* N1 A7 F, N4 B    for url_pattern in reversed(self.url_patterns):& b# |* M$ G7 N" l. Z3 B
TypeError: 'set' object is not reversible; M- }' j% k/ d5 w0 w& o9 F, U' b# l; r
[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834
" Y; R% f7 G$ ?3 {* {' c) `, W/ \% {) p9 r* }

7 \$ @$ H4 Q* @# T/ D问题的原因找到了,是因为根路径下的urls.py 文件中0 i- L; n% r" y; B# H+ b8 R) ^  h0 h

5 k# H& q' a0 _0 _8 Vurlpatterns = {
+ Z9 `; M1 k$ b! i  ^$ W! B    path('admin/', admin.site.urls),
/ C: Q4 I$ `7 x; V3 l    path('hello/', hello_world),
/ \% n' b  t2 D; r    path('hello/', include('hello.urls')),
+ z$ t2 h/ W5 U1 V& ?}
6 y! j% D" Y+ P& B9 @1 {需要改成+ M* f) J0 Z9 o- R  Z
urlpatterns = [
: B2 @3 d8 L1 h) h    path('admin/', admin.site.urls),5 \: N2 }% E' c" |' B
    path('hello/', hello_world),
% f, D+ T% L# f  c% F    path('hello/', include('hello.urls')),
2 `( H$ n7 H& i1 {9 s], I, i" @9 I* J" n0 D
问题完美解决。
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

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

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

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

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

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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