找回密码
 注册
查看: 162|回复: 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: ~7 ~" V  L7 q7 S1 D9 ?" ^2 l
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 +00004 {/ k5 y2 ~1 N/ c5 ]5 _, d
在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...
) X" O2 D% F; ^* C4 Y
; O) ^3 R0 P" w; S) D) ~
* r& L2 j, j$ ~
python调试代码出错;
! ?& v  E- ^, E4 `
! o2 D1 E1 S6 {. P9 ~
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')
0 Q! D# Z( b( H# W8 H" ?
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。0 w* L, C, N& `; g: I
怀疑自己的配置有问题,各种测试好像找不到问题所在。; }2 W0 f9 b9 ?' F3 i/ Y
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world'); o5 y3 Z& ^. m4 r( z  {4 z
再次访问,既然不报错了。正常反馈结构。# X$ c/ [- M0 F, d3 C$ {2 z1 ]1 ]7 w
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11
& {! n$ e' d1 ~& U5 g6 T
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?2 d. ]) h6 Y) K5 r" X

* h1 E/ C/ h6 I/ L

; e5 C3 `0 ^3 w1 B8 Q

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面, K- w3 F8 @' X; x0 G
def hello_world(request):
1 f8 x8 t* w, H) o" c4 }3 V    print('reverse(hello_world)')
/ G9 y0 B& Q) ?3 ?& R; P8 k    return HttpResponse('hello world')) f; m* a7 E! z, C8 {% w3 h

" D8 }# l- V% K( r+ d! c
# K) c) p7 j4 i# w8 b# y8 wdef hello_world(request):
# [* D( [3 p: F  s9 Z9 [$ R    'reverse(hello_world)'
! }6 ]- Z2 j8 k    return HttpResponse('hello world')9 Z+ ~$ g1 \/ h/ ]

6 W+ [) T" j# a/ i9 _6 X% ^7 O& A% @2 _4 b( d
2 j/ g9 D4 I. Q& D3 `3 v: p% l' N) I
[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 11' }  L0 R, ~7 [# H4 G# e7 h& P

) j$ B" h' W3 Q; l% h4 H5 l4 s6 ]) c; F' M; x
reverse(hello_world)7 ?7 z. z! }1 U6 P% H6 Q

( {0 l- B! \. c: X1 Z" J
, K7 f/ H4 m; f- M( d7 [% [9 v; G7 k  }  G5 F1 S% A* l0 |
或者这样子写也行:6 }* ]( b0 _  B& x& X& K
def hello_world(request):
, l* `& C5 T4 t6 {3 J    reverse1 = 'reverse(hello_world)'
8 X. Z2 z' P- T* K2 a    print(reverse1)
  w' ^. d- o& S    return HttpResponse('hello world')$ O/ b5 c4 P: i

" `; E" Z) X+ A9 E4 h8 U都可以获取到逆向解析,并且成功。! r5 D% N3 I: w2 G5 F. f

* t! b) e; \. J7 U2 ?6 q4 |$ |) h

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/6 c9 |9 K" R; d5 ^/ L+ F' f
Traceback (most recent call last):- F8 `: N( L) }6 d( T( `* q
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
1 b1 Z" W" G6 I+ W: q. O# C/ C2 Y& j# M    response = get_response(request)5 ~: F3 }7 o# I8 p
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response1 z1 f! f, E! y; \8 F; c
    response = self.process_exception_by_middleware(e, request)
1 P3 C# y# P; m! z: G  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
9 `6 y, M. ~' P7 a2 S" j    response = wrapped_callback(request, *callback_args, **callback_kwargs)' F! {  o" O& `& r% e* Z) ]
  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china
. `. R+ r% C/ U- a- G. e5 [    url= reverse('hello_china')( W4 \7 ~) Z5 ?1 E3 Q; Z
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse4 U1 z8 S8 Z/ w3 D- ?' G1 U
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))0 B2 C/ @' Z6 ~, p
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix
; R3 f. _7 A7 D# [' c    self._populate()
+ T, ~" O& j# `6 U& S: W+ P% f  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate
( m0 s( K9 C/ |3 o. P# @    for url_pattern in reversed(self.url_patterns):
4 Y' ~7 B5 z" W$ K; c9 ~* C/ P! K* GTypeError: 'set' object is not reversible8 e% Y' }4 Y9 }5 B3 o, I4 e
[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834
% {" h, H' u- A0 f: K; _0 H
3 }5 ?- L5 y! A* x2 g/ O
" D+ n. q5 z; q% b3 v, n0 V% V' ?问题的原因找到了,是因为根路径下的urls.py 文件中
6 a6 c8 ?1 b7 T, O$ [5 _! O: ]2 b! A
urlpatterns = {% q7 L$ P/ E3 [$ I" E. W  A, P
    path('admin/', admin.site.urls),
( n6 ?" h  d3 B    path('hello/', hello_world),
, i3 R& [7 t4 k4 l6 y    path('hello/', include('hello.urls')),4 H1 x4 i" B( y+ I) M5 D
}
# |" g; K) u7 Y8 D6 z1 H需要改成6 G5 z2 b5 n; ^- e  o5 ~3 [
urlpatterns = [
( W& Z/ _: V: @& V7 `5 ?/ ]    path('admin/', admin.site.urls),
' X/ q2 \# F# N* _* O2 q8 ?" W, M    path('hello/', hello_world),% b* J* \1 R6 w' f) G
    path('hello/', include('hello.urls')),, P; O: T! E6 ?0 |
]
( {4 y" J; O9 \& g* Z9 F, O5 v问题完美解决。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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