NoReverseMatch位于/清单 [英] NoReverseMatch at / list
问题描述
我尝试获取动态链接
错误:
/
找不到带有关键字参数'{'pk':1}'的'new_single'.尝试了1个模式:['single/']
Reverse for 'new_single' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['single/']
代码:查看:
{% for new in news %}
{{ new.id }}
<h2><a href="{% url 'new_single' pk=new.id %}">{{ new.title }}</a></h2>
{% endfor %}
网址:
urlpatterns = [
url(r'^$', views.news_list, name='news_list'),
url(r'single/<int:pk>', views.new_single, name="new_single"),
]
观看次数:
def new_single(request,pk):
new=get_object_or_404(News,id=pk)
return render(request,"news/news_single.html",{"new":new})
推荐答案
您正在混合使用两种语法变体来指定模式.由于 django-2.0 ,所以有两个指定URL模式的方法:使用 path(..)
[Django-doc] ,并且使用 re_path(..)
[Django-doc] 用于类似正则表达式的模式(别名为 url(..)
[Django-doc] ).
You are mixing two syntax variants to specify patterns. Since django-2.0 there are two ways to specify URL patterns: with path(..)
[Django-doc], and with re_path(..)
[Django-doc] for regular expressions-like patterns (an alias is url(..)
[Django-doc]).
但是您将两者混为一谈.您可以同时使用两者,但是您需要指定 per urlpatterns
条目中正确的一项:
You however mix the two. You can use the two concurrently, but you need to specify per urlpatterns
entry the correct one:
# app/urls.py
from django.urls import path, re
urlpatterns = [
url(r'^$', views.news_list, name='news_list'),
path('single/<int:pk>/', views.new_single, name="new_single"),
]
这篇关于NoReverseMatch位于/清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!