【问题标题】:NoReverseMatch error . Reverse for '...' not foundNoReverseMatch 错误。未找到“...”的反向
【发布时间】:2020-12-19 01:19:06
【问题描述】:

发生这种情况时,我试图在 Django 中实现动态 url

在我的 template.py 中,我添加了这一行

 <a href="{% url 'Index' %}" role="button">Go to Index</a>

我的 urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("moviez.urls"))
]

我的电影z.urls.py

from django.urls import path
from .views import IndexView

app_name = "moviez"

urlpatterns = [
    path("", IndexView, name="Index")
]

我认为这绝对应该可以工作,但它返回了这个错误

NoReverseMatch at /
Reverse for 'Index' not found. 'Index' is not a valid view function or pattern name.

你能帮我调试一下吗?

任何帮助将不胜感激!

【问题讨论】:

    标签: python django django-urls django-url-reverse


    【解决方案1】:

    由于您定义了app_name,因此您需要将其添加为前缀,以冒号分隔(:),因此:

    &lt;a href="{% url '<b>moviez:</b>Index' %}" role="button"&gt;Go to Index&lt;/a&gt;

    有关详细信息,请参阅URL namespaces and included URLconfs section of the documentation。

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2023-03-21
      • 2014-01-08
      • 2021-03-01
      • 2021-08-11
      • 2013-10-05
      相关资源
      最近更新 更多