如何从列表模板访问 Django 2 中的详细信息模板?

How to access from the list template to the detail template in Django 2?

我正在尝试从列表模板访问到详细信息模板

示例:

文章:网址

article_patterns = [
  path('', HomePageView.as_view(), name='home')
  path('articles/', ArticleListView.as_view(), name="articles"),
  path('articles/<int:pk>/', ArticleDetailView.as_view(), name="article"),
]

主要网址

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

article_list.html

<a href="{% url 'articles:article' article.id %}"> Detail {{articles.title}}</a>

但是我有这个错误:articles is not a registered namespace

有什么想法或建议吗?

您可以按照 floydya 的说明进行操作并忽略命名空间。但是如果你想包含它,你应该在 main.py.

的 include 函数中添加一个 namespace 参数
path('', include(article_patterns, namespace="articles"))

编辑:

如果您想采用这种方法并使用 namespace 参数,您需要遵循 rawken 的回答中给出的建议。也就是说,在您的文章应用程序中设置一个 urls.py 文件(如果有的话),然后包含 urls 模块,而不仅仅是视图列表。如果您想查看所有选项,请参阅包含函数的 source code