【问题标题】:NoReverseMatch at /colorsets/new/ Reverse for 'user_logout' not found. 'user_logout' is not a valid view function or pattern nameNoReverseMatch at /colorsets/new/ Reverse for 'user_logout' 未找到。 “user_logout”不是有效的视图函数或模式名称
【发布时间】:2017-08-28 14:16:26
【问题描述】:

我在尝试单击“新颜色集”按钮时遇到此错误:

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

我通过 StackOverflow 和其他网站上的其他地方进行了广泛的查看,但似乎无法找到问题所在。据我所知,我的所有代码都是正确的,但显然存在问题。

base.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Colors</title>

        <meta name"viewport" content="width=device-width, initial-scale=1">
        <meta charset="uft-8">
        <link rel="shortcut icon" href="/images/favicon.ico">
        <link rel="stylesheet" href="/style.css">
    </head>

<body>

    <nav>
        <div class="container">
            <a class="btn" href="{% url 'index' %}">Home</a>
            {% if user.is_authenticated %}
                <a class="btn" href="{% url 'colorsets:new_color' %}">New Color Set</a>
                <a class="btn" href="{% url 'accounts:user_logout' %}">Logout</a>
            {% else %}
                <a class="btn" href="{% url 'accounts:user_login' %}">Login</a>
                <a class="btn" href="{% url 'accounts:register' %}">Register</a>
            {% endif %}
        </div>
    </nav>

    <div class="container">
        {% block content %}
        {% endblock %}
    </div>

</body>

</html>

Views.py

from django.shortcuts import render
from accounts.forms import UserForm

from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required

# Create your views here.
def user_login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username,password=password)

        if user:
            if user.is_active:
                login(request,user)
                return HttpResponseRedirect(reverse('index'))
            else:
                return HttpResponse("Account now active")

        else:
            print("Login Unsuccessful")
            return HttpResponse("Your username and/or password are not correct")

    else:
        return render(request,'accounts/login.html',{})

def register(request):
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)

        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)

            registered = True
        else:
            print(user_form.errors)

    else:
        user_form = UserForm()

    return render(request,'accounts/register.html',{'user_form':user_form,'registered':registered})

@login_required
def user_logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('index'))

accounts app urls.py

    from django.conf.urls import url
    from accounts import views

    app_name = 'accounts'

    urlpatterns = [
        url(r'^register/$',views.register,name='register'),
        url(r'^login/$',views.user_login,name='user_login'),
        url(r'^logout/',views.user_logout,name='user_logout'),
    ]

项目 urls.py

"""colors URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from accounts import views
from colorsets import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.index,name='index'),
    url(r'^accounts/',include('accounts.urls',namespace='accounts')),
     url(r'^colorsets/',include('colorsets.urls',namespace='colorsets')),
]

如果您需要查看其他内容,请告诉我。

【问题讨论】:

    标签: python django


    【解决方案1】:

    问题出在 colorsets 命名空间中名为 new_color 的 url 下的模板中。您肯定将其用作{% url 'user_logout' %},而您应该将其用作{% url 'accounts:user_logout' %}。只需添加命名空间。

    【讨论】:

    • 我在模板中只看到一个对user_logout url 的引用,并且它已经包含了命名空间。
    • 我只使用了一次user_logout(在我的 base.html 中),它确实有命名空间“accounts”
    • 是的,因为当您尝试访问基本上位于您的colorsets django 应用程序中的/colorsets/new/ 页面时,就会出现问题。您应该在名为new_color 的colorsets/urls.py 中搜索一个url。然后找到对应的模板,在该模板中你应该找到错误的反向url{% url 'user_logout' %}并将其更改为{% url 'accounts:user_logout' %}。如果您自己找不到,只需将该模板添加到问题帖子中即可。
    【解决方案2】:

    在你的项目中检查这两件事:

    -在设置文件中你应该有这两个代码

    LOGIN_REDIRECT_URL = 'home'
    LOGOUT_REDIRECT_URL = 'home'
    

    -项目的urls.py文件中必须有路径(如下图)

    path('users/', include('django.contrib.auth.urls')),  
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2020-01-07
      • 1970-01-01
      • 2019-05-19
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多