【问题标题】:NoReverseMatch at / Reverse for 'store' not found. 'store' is not a valid view function or pattern nameNoReverseMatch at / Reverse for 'store' 未找到。 'store' 不是有效的视图函数或模式名称
【发布时间】:2019-03-09 06:17:15
【问题描述】:

我正在尝试制作一个简单的主页,其中包含指向另一个名为 store.html 的页面的链接。

views.py

from django.shortcuts import render, get_object_or_404, redirect

def home(request):
    return render(request, 'store/home.html')

def store(request):
    return render(request, 'store/store.html')

urls.py

from django.urls import path
from . import views


app_name = 'store'

urlpatterns = [
    path('', views.home, name ='home'),
    path('store/', views.store, name = 'store'),
]

home.html

我在这里放了href="{% url 'home' %}",但我发现reverse not match 错误,除此之外一切都很好。

{% extends './base.html' %}

{% block content %}
<body>
    <h1>home</h1>
    <a class = 'btn' href="{% url 'store' %}">store</a>

</body>
{% endblock %}

请帮助我如何使用修复此问题来制作链接,以便在单击该链接时将打开另一个页面。我是django 的新手并使用version 2.1.7

【问题讨论】:

  • name ambiguity` 应该避免。你有 app_namepath name 作为 store 应该是不同的。
  • 试试&lt;a class = 'btn' href="{% url 'store:store' %}"&gt;store&lt;/a&gt;
  • 好的,谢谢它的工作。

标签: html django


【解决方案1】:

根据您的错误提示,store 不是有效的视图函数,因为您在 urls.py 中添加了 app_name,

<body>
    <h1>home</h1>
    <a class = 'btn' href="{% url 'store:store' %}">store</a>
</body>

当您在 urlpatterns 中提到它时,Url 将始终需要 app_name。要了解更多信息,请查看Django Namespace

【讨论】:

  • @BikiDeka 您可以将此答案标记为正确,以供其他人将来参考。
猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 2021-12-25
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2021-06-23
  • 1970-01-01
相关资源
最近更新 更多