我在尝试点击"新颜色集"时遇到此错误按钮:
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')),
]
如果您需要查看其他内容,请与我们联系。
答案 0 :(得分:3)
问题出在new_color
命名空间中名为colorsets
的网址下的模板中。您肯定将其用作{% url 'user_logout' %}
,而您应该像{% url 'accounts:user_logout' %}
一样使用它。只需添加名称空间。
答案 1 :(得分:0)
-在设置文件中,您应该具有这两个代码
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
-项目的urls.py文件中必须具有路径(如下所示)
path('users/', include('django.contrib.auth.urls')),