How do I add link to another page [Django 3.0]? Reverse not found

旧城冷巷雨未停 提交于 2021-01-24 09:43:21

问题


I'm trying to add a link to blogDetails.html on the main page, but using the {% url '...' %} template tag is raising a Reverse not found exception.

index.html

<a href="{% url 'blogDetails' %}">O blogu</a>

urls.py

path('blogDetails/', views.BlogDetailsPageView.as_view(), name='blogDetails'),

views.py

class BlogDetailsPageView(TemplateView): 
    template_name = 'blog/blogDetails.html'

main urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls', namespace='blog')),
]

This is the error I get:

Reverse for 'blogDetails' not found. 'blogDetails' is not a valid view function or pattern name.

What on earth is going on here? All help appreciated.


回答1:


In your urlpatterns you use namespace='blog'. Looking at the django documentation for the {% url %} template tag, it says:

If you’d like to retrieve a namespaced URL, specify the fully qualified name:

{% url 'myapp:view-name' %}

If you add the blog namespace to your templatetag call, it should work:

<a href="{% url 'blog:blogDetails' %}">O blogu</a>


来源:https://stackoverflow.com/questions/59274477/how-do-i-add-link-to-another-page-django-3-0-reverse-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!