我收到了下一个NoReverseMatch错误:
NoReverseMatch at /
Reverse for 'logout' not found. 'logout' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'logout' not found. 'logout' is not a valid view function or pattern name.
Exception Location: /home/ivan/.local/lib/python2.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable: /usr/bin/python
Python Version: 2.7.12
Python Path:
['/home/ivan/My_website/essostrade (copy 1)',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/ivan/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
Server time: Fri, 25 Aug 2017 22:51:14 +0000
但问题是模板中突出显示的main.js行中没有“注销”,也没有模板中的其他位置。
以下是我的模板: main.html中:
{% extends "base.html" %}
{% load staticfiles %}
{% block additional_css %}
<link type="text/css" href="{% static "main.css" %}" rel="stylesheet">
{% endblock %}
{% block content %}
<div id="header">
<h1>Браавосская лавочка</h1>
</div>
<!-- <div id="leftmenu">
<ul>
{% url "main" as page_url %}
<li><a href="{{ page_url }}"{% if page_url == current_url %} class="current"{% endif %}>Главная</a></li>
{% if user.is_authenticated %}
<li class="indented"><a href="/admin/">Админка</a></li>
<li class="indented"><a href="{% url "logout" %}">Выйти</a></li>
{% endif %}
</ul>
</div> -->
<div id="main">
{% block main %}
{% endblock %}
</div>
<div id="footer">
<p>Все права принадлежат Матери Драконов</p>
</div>
{% endblock %}
mainpage.html:
{% extends "main.html" %}
{% block title %}Главная страница{% endblock %}
{% block main %}
<h2>Удивительные товары со всего Вестероса</h2>
<p>Фирма веников не вяжет, фирма делает гробы</p>
{% endblock %}
这是我的views.py文件:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.views.generic import TemplateView
from generic.mixins import CategoryListMixin
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django import forms
class MainPageView(TemplateView, CategoryListMixin):
template_name = "mainpage.html"
class LoginView(TemplateView):
template_name = "login_logout.html"
isLoggedIn = False
class PageNumberMixin(CategoryListMixin):
def get_context_data(self, **kwargs):
context = super(PageNumberMixin, self).get_context_data(**kwargs)
try:
context["pn"] = self.request.GET["page"]
except KeyError:
context["pn"] = "1"
return context
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'signup.html', {'form': form})
这是我的urls.py文件:
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import include
from main.views import LoginView, MainPageView
from main.views import signup
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', MainPageView.as_view(), name='main'),
url(r'^signup/$', signup, name='signup'),
]
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
我真的不知道这个'退出'出现在哪里。
答案 0 :(得分:0)
您的{% url "logout" %}
from django.contrib.auth.views import LogoutView
url(r'^logout/$', LogoutView.as_view(), name='logout'),
模板正在尝试撤消网址注销,但您还没有为注销视图添加网址格式。
registration/logged_out.html
您必须创建一个LOGOUT_REDIRECT_URL
模板才能显示,或在您的设置中设置if(object != null){
//do something
}
。