【问题标题】:How to access from the list template to the detail template in Django 2?如何从列表模板访问 Django 2 中的详细信息模板?
【发布时间】:2019-02-22 14:37:22
【问题描述】:

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

示例:

文章:网址

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

&lt;a href="{% url 'articles:article' article.id %}"&gt; Detail {{articles.title}}&lt;/a&gt;

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

有什么想法或建议吗?

【问题讨论】:

  • 请提供所有 urls.py 代码
  • 只使用{% url 'article' article.id %} 而不使用articles: 命名空间。
  • 因为里面有类似 app_name 的东西
  • 谢谢@floydya 它运作良好

标签: django django-templates python-3.6 django-2.0


【解决方案1】:

您可以按照 floydya 所说的去做而忽略命名空间。但是如果你想包含它,你应该在 main.py 中的 include 函数中添加一个 namespace 参数。

path('', include(article_patterns, namespace="articles"))

编辑:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多