找回密码
 注册
查看: 159|回复: 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 reversible6 w7 @% N; E, s# O# O4 _
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
3 k+ i' A( S# ^3 N3 A, R
在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...
5 @: S. m$ Y& N/ ^6 Z
2 K7 e4 Y* I: H* M) `: e5 h

* N% t1 [* @* E) B9 Epython调试代码出错;5 z# c* U* `% @, C

4 E" \, c+ ?4 u" }( N7 f0 j
代码如下:def hello_world(request):    print(reverse('hello_world'))    return HttpResponse('hello world')  u5 p( s  V0 q( Q
这个时我们在测试逆向解析的时候报的。怎么都不知道哪儿错了。3 @" k7 s1 N6 {) H$ n3 {; |) |% k
怀疑自己的配置有问题,各种测试好像找不到问题所在。1 V; [  ]4 O( v7 }9 }
然后想着这个是不是引号的位置不对,带着这个想法修改下def hello_world(request):    print('reverse(hello_world)')    return HttpResponse('hello world')
; \& t( c3 S# ]8 v; `! X$ Q: V
再次访问,既然不报错了。正常反馈结构。
3 N7 P4 u' r6 C, ]+ Z
并在控制台上返回结果为:reverse(hello_world)[18/Mar/2025 21:41:27] "GET /hello/world/ HTTP/1.1" 200 11
% U  z! `( r9 z) [3 G5 w- Z3 [: h
问题暂时解决了,但想不通为啥和老师讲的做的有些区别,难道是因为pycharm版本不一致导致的吗?
  b. q" |/ }  I, p2 u/ e2 ]8 }
" \: ^3 {8 X1 v/ v8 |7 S( {
) D7 R# |8 s( T: |" y( k  P

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-4-19 08:17:05 | 显示全部楼层
视图函数改成单引号在外面" c" g5 E% A1 T! J1 I/ ]
def hello_world(request):
% `* _, v/ G, {1 K    print('reverse(hello_world)')* i  T1 z0 D& f9 P: c* {3 u& p" e
    return HttpResponse('hello world')7 h, }* ~/ F+ ~1 y; ~
. Q: U2 T7 K  u2 k% u, F5 d+ m

4 n- Q) T# h- z6 N/ N/ g' }def hello_world(request):, j# m/ L. g# S: B) i& s
    'reverse(hello_world)'
' q! _. Y$ h/ y; E- l  t    return HttpResponse('hello world')" W3 r4 }% {0 F$ e
  i$ n% [; ^/ l% T5 x! f

* n. x, I' C9 A- H8 W9 R7 L7 |0 w) o1 ~3 V+ F7 |6 T9 V
[19/Mar/2025 08:12:55] "GET /hello/world/ HTTP/1.1" 200 11
1 {/ O7 c  @8 H/ i. j# d, l9 y9 @# {+ m* z9 s6 s

) S+ k" N/ i2 `, I. k) @reverse(hello_world)
6 K! I" |# k* |% a: j7 Z4 {) c$ `4 R

2 C  H6 \# k1 V; n8 T, {. T
! x% F5 }0 V7 K" ?4 c9 V或者这样子写也行:& I% d; X5 U2 D, G
def hello_world(request):
- X4 g* l% S/ v/ N( D. f    reverse1 = 'reverse(hello_world)'
1 A1 T" d: L+ M( M6 v0 i    print(reverse1)* Q; Z6 {& P; ~: Z  ~
    return HttpResponse('hello world')
5 r; g! b) e( l8 g  v* L2 O8 J( R/ v3 I% Y+ \- w* E
都可以获取到逆向解析,并且成功。
( |' t* x6 j9 V. o/ X/ V$ W1 r. @& P& u* `8 i. l0 \1 s

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/
2 b* S( d" w, ~  _% G# g) D$ n3 e5 `Traceback (most recent call last):; Q5 A5 L8 p! ?/ r1 K$ W$ F. o
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
. D. ]0 Q3 M% m5 T! _* M& h    response = get_response(request)
9 D" P7 R& ~3 M) V  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response3 r; L6 B# x! a/ t% P
    response = self.process_exception_by_middleware(e, request)
3 y& X: W( C$ \; r" @  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
3 s- |% k% b# d3 w    response = wrapped_callback(request, *callback_args, **callback_kwargs)  f6 ~7 k5 @# L0 A
  File "C:\Users\admin\Desktop\testmyproject\hello\views.py", line 11, in hello_china2 g7 c& h' K* w' ~
    url= reverse('hello_china')  x3 U( E( a( b# `! K
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\base.py", line 87, in reverse
* w1 m  A2 o5 h4 U8 H# }    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))5 X+ `. d; w, ]8 ^9 L- e! K
  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 617, in _reverse_with_prefix
5 l' o8 j9 n5 Q: `    self._populate()
- F6 I' x7 I0 X% L9 Q8 F0 e! Z  File "C:\Users\admin\python3.8.2-vitual\lib\site-packages\django\urls\resolvers.py", line 455, in _populate
( t! r. [1 {$ Z0 J    for url_pattern in reversed(self.url_patterns):
7 o$ r  |3 M: V% @8 X. h  e! }TypeError: 'set' object is not reversible
0 G, W$ t: J8 t2 S. V0 U[23/Mar/2025 22:26:12] "GET /hello/china/ HTTP/1.1" 500 81834
6 w" R4 w5 o8 Q3 n+ V2 {: h, z! g% r, W, N! d

) j. V# I! a) N% O3 U4 k# _问题的原因找到了,是因为根路径下的urls.py 文件中
6 }9 ~1 U" a5 P- ^4 j! N  h
) r  k- C4 W$ I9 furlpatterns = {
, w2 P/ U4 A2 }# [9 u" c9 H    path('admin/', admin.site.urls),/ g7 S8 Y+ I2 s8 s
    path('hello/', hello_world),
. D# A4 p0 m7 I# g7 B    path('hello/', include('hello.urls')),7 M* v" F  Y! }' o$ e2 ?9 H, X2 r
}2 R1 X6 }) L% U. l9 F6 i
需要改成2 J! R3 d6 A( m8 N4 ?, H
urlpatterns = [
$ n/ L) Y0 K8 p/ Z; f0 Y/ u    path('admin/', admin.site.urls),5 e* ?* R& a3 t2 u5 d) g
    path('hello/', hello_world),( f" d0 E; i$ h/ M% N
    path('hello/', include('hello.urls')),
: d. q# k' ^1 v9 k) W4 s% D]7 u6 z, j! {+ n( A
问题完美解决。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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