NoReverseMatch 在 /account/

NoReverseMatch at /account/

我很难调试这个错误...

错误是这样的:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/account/

Django Version: 2.0.3
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'talk',
 'users']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/hun/Documents/TalkingBuddy/TalkingBuddy/talk/templates/talk/base.html, error at line 0
   Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
   1 : <!DOCTYPE html>
   2 : <html>
   3 :     <head>
   4 :        {% block head %} {% endblock %}
   5 :        <link href="https://fonts.googleapis.com/css?family=Poppins:300&display=swap" rel="stylesheet"/>
   6 :     </head>
   7 :     <body>
   8 :         {% block body %}
   9 :         {% endblock %}
   10 :     </body>


Traceback:

File "/home/hun/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/hun/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/hun/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/hun/Documents/TalkingBuddy/TalkingBuddy/users/views.py" in account
  19.     return render_to_response('users/account.html')

File "/home/hun/.local/lib/python3.6/site-packages/django/shortcuts.py" in render_to_response
  27.     content = loader.render_to_string(template_name, context, using=using)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/home/meina/.local/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/hun/.local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/home/hun/.local/lib/python3.6/site-packages/django/urls/base.py" in reverse
  88.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "/home/hun/.local/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  632.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /account/
Exception Value: Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

这是我的 urls.py

代码
from django.contrib import admin
from django.urls import include, path
from talk import views as v_talk
from users import views as v_users

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', v_talk.index, name='home'),
    path('register/', v_users.register, name='register'),
    path('account/', v_users.account, name='account'),
    path("", include("talk.urls")),
    path('', include("django.contrib.auth.urls")),
    
]

这是我在 views.py 中的帐户代码:

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render, redirect, render_to_response
from .forms import RegisterForm
from django.urls import reverse


# Create your views here.
def register(response):
    if response.method == "POST":
        form = RegisterForm(response.POST)
        if form.is_valid():
            form.save() 
            return redirect('account')   
    else:
        form = RegisterForm()
    return render(response, 'users/register.html', {"form":form})

def account(request):
    return render_to_response('users/account.html')

这是我的 account.html 代码:

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

{% block head %}
    <link rel="stylesheet" href="../../static/css/styles.css">
    <title>Home</title>
{% endblock %}

{% load crispy_forms_tags %}

{% block body %}
    <nav>
        <ul class="nav-bar">
            <li><a href="{% url 'index' %}" class="home-button">Home</a></li>
        </ul>
    </nav>
    <p class="welcome-login-text">
        Welcome back!
    </p>
{% endblock %}

任何帮助将不胜感激。当我将 return redirect('account') 从 views.py 更改为 return redirect('home') 它有效,这意味着错误在帐户中,但我找不到它并开始失去希望......

实际上问题出在模板 <a href="{% url 'index' %}" class="home-button">Home</a> 中的 a tag 中,您在这里调用 index ,但在您的 url 中,您将其命名为 homepath('home/', v_talk.index, name='home')。因此,要么将您的 url 名称更改为索引,即path('home/', v_talk.index, name='index') 或用 home 替换标签中的索引,即 <a href="{% url 'home' %}" class="home-button">Home</a>

作为错误状态

Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

您没有在具有 name=index 的网址中设置路径,罪魁祸首是以下行

    <li><a href="{% url 'index' %}" class="home-button">Home</a></li>