-1

I am getting the error NoReverseMatch at / when I am trying to link my article to article detail page.

NoReverseMatch at / Reverse for 'article-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['article\/(?P[0-9]+)\/$']

    {% for articles in object_list %}
    <div class="article">
        <div class="article-title">
            <a href="{% url 'article-detail' pk=article.pk %}"></a>
                <p class="p-title">{{ articles.title }}</p>
            </a>
        </div>
        <div class="article-content">
            <p>{{ articles.content }}</p>
        </div>
    </div>
    <hr>
    {% endfor %}
    path('', views.HomeView.as_view(), name='index'),

    path('article/create/', views.ArticleCreateView.as_view(), name='article-create'),
    path('article/<int:pk>/update/', views.ArticleUpdateView.as_view(), name='article-update'),
    path('article/<int:pk>/', views.ArticleDetailView.as_view(), name='article-detail'),
]```


3 Answers 3

2

There is a typo

Change this

<a href="{% url 'article-detail' pk=article.pk %}"></a>

to

<a href="{% url 'article-detail' pk=articles.pk %}"></a>
0

You need to do this:

<a href="{% url 'article-detail' articles.pk %}"></a>

No need for pk= and you have a typo

0

If you read the error message, you'll find out that reverse was called with the empty string as 'pk' value, which means the template system could not resolve article.pk (=> either there was no context variable named 'article' or there was one but it didn't have a "pk" attribute or key or method). Then if you re-read your template code, you can see that you named your forloop iteration variable articles (plural), not article (singular), hence the error.

And the conclusions are 1/ that you should always carefully read the error message and 2/ that a good, sensible and consistent naming avoids a whole lot of typos...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.