如何将链接添加到另一个页面[Django3.0]?找不到反向

2024-09-29 22:00:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在主页上添加到blogDetails.html的链接,但是使用{% url '...' %}模板标记引发了一个反向未找到异常。你知道吗

索引.html

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

网址.py

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

视图.py

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

主网址.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.

这到底是怎么回事?感谢大家的帮助。你知道吗


Tags: pathnamepyviewurladmin链接html
1条回答
网友
1楼 · 发布于 2024-09-29 22:00:19

urlpatterns中使用namespace='blog'。看看the django documentation for the ^{} template tag,它说:

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

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

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

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

相关问题 更多 >

    热门问题