问题
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