如何将 link 添加到另一个页面 [Django 3.0]?反向未找到
How do I add link to another page [Django 3.0]? Reverse not found
我正在尝试在主页上将 link 添加到 blogDetails.html
,但是使用 {% url '...' %}
模板标签引发了 Reverse not found异常。
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'
主要urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]
这是我得到的错误:
Reverse for 'blogDetails' not found. 'blogDetails' is not a valid view function or pattern name.
这到底是怎么回事?感谢所有帮助。
在你的 urlpatterns
中你使用 namespace='blog'
。查看 the django documentation for the {% url %}
template tag,它说:
If you’d like to retrieve a namespaced URL, specify the fully qualified name:
{% url 'myapp:view-name' %}
如果您将 blog 命名空间添加到您的模板标签调用中,它应该可以工作:
<a href="{% url 'blog:blogDetails' %}">O blogu</a>
我正在尝试在主页上将 link 添加到 blogDetails.html
,但是使用 {% url '...' %}
模板标签引发了 Reverse not found异常。
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'
主要urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]
这是我得到的错误:
Reverse for 'blogDetails' not found. 'blogDetails' is not a valid view function or pattern name.
这到底是怎么回事?感谢所有帮助。
在你的 urlpatterns
中你使用 namespace='blog'
。查看 the django documentation for the {% url %}
template tag,它说:
If you’d like to retrieve a namespaced URL, specify the fully qualified name:
{% url 'myapp:view-name' %}
如果您将 blog 命名空间添加到您的模板标签调用中,它应该可以工作:
<a href="{% url 'blog:blogDetails' %}">O blogu</a>