Python 未找到页面:/<;int:pk>;。主键
有人能告诉我为什么通过点击模板链接生成的URL是/.pk吗。我试图理解URL是如何工作的。这里是Django的新手Python 未找到页面:/<;int:pk>;。主键,python,django,Python,Django,有人能告诉我为什么通过点击模板链接生成的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, clie
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
与上一个问题一样,您混淆了
path()
的新语法和re\u path()
/url()
的正则表达式语法
path()
不接受正则表达式,因此不应在末尾包含$
。将其更改为:
path('<int:pk>/', views.detail, name='details'),
对代码/模板进行更改后,请确保已保存所有更改并重新启动服务器,以确保正在运行您认为正确的代码。删除
$
。
urlpatterns = [
...
path('<int:pk>/$', views.detail, name='details'),
]
def details(self, pk):
print('1')
testimony=get_object_or_404(Testimony, pk= pk)
print('2')
return render(request, 'details.html', {'testimony': testimony})
path('<int:pk>/', views.detail, name='details'),
<h1><a href="{% url 'details' pk=testimony.id %}">{{testimony.Title}}</h1>