NoReverseMatch 错误。找不到“...”的反向

NoReverseMatch error . Reverse for '...' not found

发生这种情况时我正在尝试在 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"))
]

我的电影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.

你能帮我调试一下吗?

任何帮助将不胜感激!

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

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

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