如何修复Django中的'NoReverseMatch at /'错误

时间:2019-06-05 10:11:13

标签: django-templates

我正在尝试在博客应用上添加一个链接,但是该链接就像您单击博客时一样,也可以看到其完整的博客和其他详细信息,但它显示了一些反向错误

我尝试使用href="{% url 'post-detail' posts.id %}"代替href="{% url 'post-detail' post.id %}",也尝试使用href="{% url 'post-detail' pk=post.id %}",但没有任何效果

我的模板: 这是代码

`{%extends 'base.html'%}
  {%block content %}
    {%for i in posts %}
    <article class="media content-section">
      <img class="rounded-circle article-img" src="{{ 
    post.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-metadata">
          <a href="#" class="mr-2">{{i.author}}</a>
          <small class="text-muted">{{i.date_posted|date:"F d Y"}}</small>
        </div>
      <h2><a class="article-title" href="{% url 'post-detail' posts.id %}"> 
   {{i.title}}</a></h2>
      <p class="article-content">{{i.content}}</p>
      </div>
      </article>
    {%endfor%}
   {% endblock content%}`

'''and thats my urls.py file'''

    `from django.urls import path
    from .views import PostListView ,PostDetailView`enter code here`
    from . import views
    urlpatterns = [
    path('',PostListView.as_view(template_name = 'home.html'),name='blog- 
           home'),
    path('post/<int:pk>/',PostDetailView.as_view(template_name = 
            'postdetail.html'),name='post-detail'),
    path('about/',views.about,name='blog-about'),
        ]
`
NoReverseMatch at /
Reverse for 'post-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/$']
Request Method:   GET
Request URL:  http://127.0.0.1:8000/
Django Version:   2.2
Exception Type:   NoReverseMatch
Exception Value:  
Reverse for 'post-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/$']

0 个答案:

没有答案