本文介绍了如何添加到另一个页面的链接[Django 3.0]?找不到反向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在首页上添加指向 blogDetails.html 的链接,但是使用 {%url'...'%} 模板标记是引发未找到反向异常.

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'

主要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.

这到底是怎么回事?感谢所有帮助.

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

推荐答案

在您的 urlpatterns 中,您使用 namespace ='blog'.查看 {%url%}的django文档模板标记,它表示:

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

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

如果将 blog 名称空间添加到templatetag调用中,则该名称空间应该可以工作:

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

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

这篇关于如何添加到另一个页面的链接[Django 3.0]?找不到反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 00:44