英文:
Django - Reverse for 'index' not found. 'index' is not a valid view function or pattern name
问题
以下是您提供的内容的翻译部分:
我是Django的新手。
我一直在使用Mozilla的模板:https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website
我创建了一个名为'debtSettler'的项目。它有一个名为'home'的应用程序。
我有以下URL映射:
./debtSettler/debtSettler/urls.py:
urlpatterns = [
path('home/', include('home.urls')),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
./debtSettler/home/urls.py:
app_name = 'home'
urlpatterns = [
path('', views.index, name='index'),
path('clubs/', views.ClubListView.as_view(), name='clubs'),
]
以及视图:
./debtSettler/home/views.py:
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
num_clubs = Club.objects.all().count()
# 默认情况下隐含使用'all()'。
num_members = Member.objects.count()
context = {
'num_clubs': num_clubs,
'num_members': num_members,
}
# 使用上下文变量中的数据呈现带有HTML模板index.html
return render(request, 'index.html', context=context)
class ClubListView(generic.ListView):
model = Club
def get_context_data(self, **kwargs):
# 首先调用基本实现以获取上下文
context = super(ClubListView, self).get_context_data(**kwargs)
# 创建任何数据并将其添加到上下文中
context['some_data'] = '这只是一些数据'
return context
在模板中,我有两个URL,它们引发错误:
<a href="{% url 'index' %}">首页</a>
<a href="{% url 'clubs' %}">所有俱乐部</a>
未找到'reverse for 'index''. 'index'不是有效的视图函数或模式名称。
如果我添加'my_app:my_view',它将按预期工作:
<a href="{% url 'home:index' %}">首页</a>
<a href="{% url 'clubs' %}">所有俱乐部</a>
但我计划在应用程序中进一步进行URL映射,因此我希望了解我在URL方面做错了什么。对我来说,我做的事情似乎与教程非常相似。
英文:
I am new to Django.
I have been working based on the template from Mozilla: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website
I have created a project called 'debtSettler'. And it has an app called 'home'.
I have the following url mappings:
./debtSettler/debtSettler/urls.py:
urlpatterns = [
path('home/', include('home.urls')),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
./debtSettler/home/urls.py:
app_name = 'home'
urlpatterns = [
path('', views.index, name='index'),
path('clubs/', views.ClubListView.as_view(), name='clubs'),
]
And views:
./debtSettler/home/views.py:
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
num_clubs = Club.objects.all().count()
# The 'all()' is implied by default.
num_members = Member.objects.count()
context = {
'num_clubs': num_clubs,
'num_members': num_members,
}
# Render the HTML template index.html with the data in the context variable
return render(request, 'index.html', context=context)
class ClubListView(generic.ListView):
model = Club
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ClubListView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['some_data'] = 'This is just some data'
return context
In the template, I have two urls that give the error:
<a href=" {% url 'index' %} ">Home</a>
<a href=" {% url 'clubs' %} ">All clubs</a>
>
> Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
If I add the my_app:my_view, it works as expected:
<a href=" {% url 'home:index' %} ">Home</a>
<a href=" {% url 'clubs' %} ">All clubs</a>
but I plan to do more of the url mapping further in the app so I want to understand what I am doing wrong with the url.
It seems to me like I am doing things very similar to the tutorial.
答案1
得分: 0
尝试在'index'之前添加"home:"
尝试这样做:
<a href=" {% url 'home:index' %} ">Home</a>
<a href=" {% url 'home:clubs' %} ">All clubs</a>
并且你应该添加命名空间
urlpatterns = [
path('home/', include('home.urls','home'),namespace = 'home'),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
英文:
try to add "home:" before'index'
try this:
<a href=" {% url 'home:index' %} ">Home</a>
<a href=" {% url 'home:clubs' %} ">All clubs</a>
And you should add namespace
urlpatterns = [
path('home/', include('home.urls','home'),namespace = 'home'),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
答案2
得分: 0
你使用了一个 app_name
,所以视图名称为"namespace"如下:
<a href="{% url '<b>home:</b>index' %}">主页</a>
<a href="{% url '<b>home:</b>clubs' %}">所有俱乐部</a>
英文:
You used an app_name
, so then the view names are "namespace" with:
<pre><code><a href="{% url '<b>home:</b>index' %}">Home</a>
<a href="{% url '<b>home:</b>clubs' %}">All clubs</a></code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论