尝试单击“新颜色集”按钮时出现此错误:
NoReverseMatch at /colorsets/new/
Reverse for 'user_logout' not found. 'user_logout' is not a valid view function or pattern name.
Run Code Online (Sandbox Code Playgroud)
我已经通过 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>
Run Code Online (Sandbox Code Playgroud)
视图.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'),
]
Run Code Online (Sandbox Code Playgroud)
项目 url.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')),
]
Run Code Online (Sandbox Code Playgroud)
如果您需要查看其他内容,请告诉我。
new_color
问题出在您的命名空间中指定的 url 下的模板中colorsets
。你肯定用它作为{% url 'user_logout' %}
,而你应该用它像{% url 'accounts:user_logout' %}
。只需添加命名空间即可。
归档时间: |
|
查看次数: |
9244 次 |
最近记录: |