gpt4 book ai didi

python - 找不到页面 :/. pk

转载 作者:行者123 更新时间:2023-12-01 08:27:46 26 4
gpt4 key购买 nike

谁能告诉我为什么点击模板链接生成的url是/.pk。我想了解 url 是如何工作的。这里是 Django 新手。

Traceback (most recent call last):
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 647, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 717, in __init__
self.handle()
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 154, in handle
handler.run(self.server.get_app())
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
Not Found: /<int:pk>.pk
[10/Jan/2019 00:18:34] "GET /%3Cint:pk%3E.pk HTTP/1.1" 404 16594

模板

<h1><a href="{% url 'details' pk=Testimony.id %}">{{testimony.Title}}</h1>

url.py

urlpatterns = [
...
path('<int:pk>/$', views.detail, name='details'),
]

View .py

def details(self, pk):
print('1')
testimony=get_object_or_404(Testimony, pk= pk)
print('2')
return render(request, 'details.html', {'testimony': testimony})

最佳答案

就像在上一个问题中一样,您对 path() 的新语法感到困惑以及 re_path() 的正则表达式语法/url() .

path()不采用正则表达式,因此您不应包含 $在最后。将其更改为:

path('<int:pk>/', views.detail, name='details'),

由于您的模板上下文是 {'testimony': testimony} ,你应该使用小写 testimony在 url 标记中:

<h1><a href="{% url 'details' pk=testimony.id %}">{{testimony.Title}}</h1>

对代码/模板进行更改后,请确保您已保存所有更改并重新启动服务器,以确保您正在运行您认为的代码。

关于python - 找不到页面 :/<int:pk>. pk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com