gpt4 book ai didi

python - NoReverseMatch at/colorsets/new/Reverse 找不到 'user_logout'。 'user_logout' 不是有效的 View 函数或模式名称

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:14 25 4
gpt4 key购买 nike

尝试单击“新颜色集”按钮时出现此错误:

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

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

基础.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>

View .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')),
]

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

最佳答案

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

关于python - NoReverseMatch at/colorsets/new/Reverse 找不到 'user_logout'。 'user_logout' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45921147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com