“Reverse for ‘index’ not found. ‘index’ is not a valid view function or pattern name.”

huangapple go评论58阅读模式
英文:

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(&#39;home/&#39;, include(&#39;home.urls&#39;)),    
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;&#39;, RedirectView.as_view(url=&#39;home/&#39;)),    
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

./debtSettler/home/urls.py:

app_name = &#39;home&#39;
urlpatterns = [    
    path(&#39;&#39;, views.index, name=&#39;index&#39;),
    path(&#39;clubs/&#39;, views.ClubListView.as_view(), name=&#39;clubs&#39;),    
]

And views:

./debtSettler/home/views.py:

from django.http import HttpResponse, HttpResponseRedirect


def index(request):

    num_clubs = Club.objects.all().count()    

   
    # The &#39;all()&#39; is implied by default.
    num_members = Member.objects.count()

    context = {
        &#39;num_clubs&#39;: num_clubs,
        &#39;num_members&#39;: num_members,        
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, &#39;index.html&#39;, 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[&#39;some_data&#39;] = &#39;This is just some data&#39;
        return context

In the template, I have two urls that give the error:

&lt;a href=&quot; {% url &#39;index&#39; %}  &quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot; {% url &#39;clubs&#39; %} &quot;&gt;All clubs&lt;/a&gt; 

>
> 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:"

尝试这样做:

&lt;a href=&quot; {% url &#39;home:index&#39; %}  &quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot; {% url &#39;home:clubs&#39; %} &quot;&gt;All clubs&lt;/a&gt; 

并且你应该添加命名空间

urlpatterns = [
   
    path(&#39;home/&#39;, include(&#39;home.urls&#39;,&#39;home&#39;),namespace = &#39;home&#39;),    
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;&#39;, RedirectView.as_view(url=&#39;home/&#39;)),    
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
英文:

try to add "home:" before'index'

try this:

&lt;a href=&quot; {% url &#39;home:index&#39; %}  &quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot; {% url &#39;home:clubs&#39; %} &quot;&gt;All clubs&lt;/a&gt; 

And you should add namespace

urlpatterns = [
   
    path(&#39;home/&#39;, include(&#39;home.urls&#39;,&#39;home&#39;),namespace = &#39;home&#39;),    
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;&#39;, RedirectView.as_view(url=&#39;home/&#39;)),    
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

答案2

得分: 0

你使用了一个 app_name,所以视图名称为"namespace"如下:

&lt;a href="{% url '<b>home:</b>index' %}"&gt;主页&lt;/a&gt;
&lt;a href="{% url '<b>home:</b>clubs' %}"&gt;所有俱乐部&lt;/a&gt;
英文:

You used an app_name, so then the view names are "namespace" with:

<pre><code>&lt;a href=&quot;{% url '<b>home:</b>index' %}&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;{% url '<b>home:</b>clubs' %}&quot;&gt;All clubs&lt;/a&gt;</code></pre>

huangapple
  • 本文由 发表于 2023年2月6日 20:20:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定