我收到了错误,
NoReverseMatch at /app/detail/3/
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['app/detail\\/(?P<pk>[0-9]+)\\/$'] .
我在views.py中写道
def top(request):
content = POST.objects.order_by('-created_at')[:5]
page = _get_page(blog_content, request.GET.get('page'))
return render(request, 'top.html',{'content':content,"page":page})
class DetailView(generic.DetailView):
model = POST
template_name = 'detail.html'
top.html中的
<div>
{% for content in page %}
<h2>{{ content.title }}</h2>
<p><a href="{% url 'detail' content.pk %}">SHOW DETAIL</a></p>
{% endfor %}
</div>
in detail.html
<div>
<h2>Comment List</h2>
<a href="{% url 'comment' pk %}">Comment</a>
{% for comment in post.comment_set.all %} Name:{{ comment.name }}
<br> Text:{{ comment.text }}
<br>
<a href="{% url 'recomment' comment.pk %}">Reply</a>
<br> {% endfor %}
</div>
<div>
<h2>Comment Form</h2>
<form action="" method="POST">
{{ form.as_p }} {% csrf_token %}
<button type="submit">Send</button>
</form>
</div>
在urls.py中
urlpatterns = [
path('top/', views.top, name='top'),
path('detail/<int:pk>/', views.DetailView.as_view(template_name='detail.html'), name='detail'),
path('comment/<int:pk>/',views.comment, name='comment'),
path('recomment/<int:pk>/', views.recomment, name='recomment'),
]
当我访问top.html时,网站显示正确,所以没关系。但是当我放置SHOW DETAIL链接时,会发生此错误。我真的不明白为什么会发生这种错误。