找回密码
 注册
查看: 160|回复: 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 reversible7 [* V; a0 z8 K. {" M& w/ v
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% b5 v2 ]8 n( |* O7 M
在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.../ R% ^, K' _3 i/ }

# g! i, D6 h7 P" F

, o" v( A  h' O) ]python调试代码出错;
6 }; U5 p! M5 S) d3 [1 M- M$ `
1 c( J5 L4 z  |  w  l- w& x
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')
6 l% m( `5 v) R" i; F+ }9 @1 |
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。) l8 T7 U7 L8 F  E5 t: W
怀疑自己的配置有问题,各种测试好像找不到问题所在。
3 O! }# }# I/ E8 w: \2 T
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world'). Q6 Z4 l1 z1 @* C& {
再次访问,既然不报错了。正常反馈结构。# ^9 Y: I- r1 b( H
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11
( j! x5 L+ G/ v: \; ~! P9 S
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?8 d4 n; v; D* \/ ~3 m# z) z
: B( t4 S& O& M" _! c9 K; q

3 z% F  ^; l) i$ r6 c2 r

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面- m/ x, b2 n( k; f/ p* Z
def hello_world(request):: f% E" Q( {- u% F7 V$ n' X  y
    print('reverse(hello_world)')* I2 g# [2 q& m9 o
    return HttpResponse('hello world')
, ^5 e4 |/ a! c
3 n$ y. r$ Z% Q+ |. U5 C2 f
: ^6 ~* l0 c8 P5 r( W2 v3 Odef hello_world(request):4 {" W# ~: v: }6 I
    'reverse(hello_world)'" z5 J8 V* h+ _( a- W
    return HttpResponse('hello world')
8 ^9 c% r" F0 ?% E9 C  d+ @" p9 }+ {  v% i9 w3 g
  T5 J( l1 u( g

7 G+ J( S* r$ E[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 11- [! Y# K1 l6 N' x
# R) {, B; r7 V. E0 {  I: S
; ]6 ?  L# P: x2 z3 ~% j
reverse(hello_world); z; ]+ `. [% o5 |6 ~- C

9 ?8 o* `/ F6 _% S, [) x+ \4 ?# E: U+ W5 |) {/ T: ?( {
) j1 H6 L+ Y* G& R' Q: x  h
或者这样子写也行:' d6 M/ r4 `- u3 Z/ \/ w1 r1 Q
def hello_world(request):% m) N& \4 h$ y- B& i" i( ]! J
    reverse1 = 'reverse(hello_world)'
6 o6 h+ }, N5 @5 P0 l    print(reverse1)
3 {; S' y, r) Z2 [    return HttpResponse('hello world')
& ~; I3 B; q0 m! j4 E
6 g: ?# J- G5 V7 S, h5 k" M0 n" L都可以获取到逆向解析,并且成功。
& M( K$ ^, L# _- X! V4 t- Q. b3 M% f$ i; H; d8 o) T7 K% P2 C: C

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/
1 d6 t" D" P/ {! nTraceback (most recent call last):
4 x) ]+ t7 ~% }  p9 ?  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner9 _0 H+ _+ f5 f) G' Y% a' ~8 V
    response = get_response(request)# y4 |: l+ M3 t
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response. J% [7 v. U" G% R/ y7 Q
    response = self.process_exception_by_middleware(e, request)
5 H9 X8 K, v/ H" W& A- M! K  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response; d; E  R1 A, E  @) r3 [' |; O
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
7 x8 e7 i. |) D3 N0 P/ E; R. K# J  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china
! ^; u8 Z( E( R* F* a1 H    url= reverse('hello_china')/ p' J$ M" [5 F2 Z3 |' n
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse
5 B' T/ h# |1 X! W# ]7 I! j    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
8 R. V4 A5 O$ b4 N  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix, p  e1 ?9 Z! H$ ~4 U
    self._populate()" q* P0 W7 }' I
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate
  C2 @" f8 e7 a% o- c. `$ w    for url_pattern in reversed(self.url_patterns):
" q( T+ ^# {  Y$ `! BTypeError: 'set' object is not reversible
/ v5 _- m) g4 r* I[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834
9 T+ }# D8 q! J, ?6 f5 _9 L
+ ]3 V: \! r2 G4 A" [3 Q6 x& G1 r8 Q5 R
问题的原因找到了,是因为根路径下的urls.py 文件中; k1 i2 d8 e5 }8 ^0 X  ?6 h# q
% W6 Y; k$ R  p, w6 n* Y" }
urlpatterns = {
$ O8 b2 _% A! T  ^6 ^; m: X    path('admin/', admin.site.urls),5 F' V, T: i6 U9 p; V& w, Y
    path('hello/', hello_world),- j: @& s; W* G" y. D3 r& y. L
    path('hello/', include('hello.urls')),
& G7 Q. _" R' l) @$ u1 H- T}4 }: ?. I: M" q2 e( t/ }& L( K
需要改成
1 t& @: }3 g7 ]! T4 xurlpatterns = [2 ^: s. d4 `3 @. |2 X' @6 {
    path('admin/', admin.site.urls),! b- y: u" z( g( y
    path('hello/', hello_world),
: f0 U) @( P' N" l0 S7 Y    path('hello/', include('hello.urls')),
, {5 Y; J0 D2 Y) l) j* V3 T]
1 B0 t9 j/ Y' P6 Z; H! `3 e$ f6 Z; |1 s问题完美解决。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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