所以我正在教自己Django并且有一个很好的项目需要一段时间。我尝试添加注册模块并进行设置,以便您可以注册该网站,只有登录的人才能看到内容页面。在这个过程中我搞砸了一些事情,现在除了管理模块页面之外,没有任何页面会加载。当我启动开发服务器时,我得到NoReverseMatch at / error。它反对' auth_logout'参数'()'和关键字参数' {}'未找到。我试图研究我能找到的任何东西,找出我出错的地方,并尝试了一些不同的配置,看看我是否可以解决它,但现在我觉得我搞砸了这么糟糕我甚至无法得到回到我开始的地方。有人可以看一看,看看他们是否可以在我造成太多伤害之前指出正确的方向吗?
这是我的views.py
from django.shortcuts import render, render_to_response, redirect
from django.views.generic import DetailView
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest, HttpResponse
from django.template import RequestContext, Context
from app.forms import OrderForm,UserForm
from datetime import datetime
from django import forms
from django.core.urlresolvers import reverse
from django.contrib.auth.mixins import LoginRequiredMixin
from app.models import Order
from django.http.response import HttpResponseRedirect
from app.models import *;
def MMIR(request):
order_list = Order.objects.all();
return render(request, 'app/MMIR.html',{'order_list':order_list});
class order_details(DetailView):
model = Order
template_name = 'app/order_details.html'
def New_Order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
model_instance = form.save(commit=False)
model_instance.save()
return HttpResponseRedirect('MMIR')
else:
form = OrderForm()
return render(request, 'app/New_Order.html', {'form': form})
def home(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
}
)
def contact(request):
"""Renders the contact page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/contact.html',
{
'title':'Contact',
'message':'Your contact page.',
'year':datetime.now().year,
}
)
def about(request):
"""Renders the about page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/about.html',
{
'title':'About',
'message':'Your application description page.',
'year':datetime.now().year,
}
)
def register(request):
context = RequestContext(request)
registered = False
if request.method == 'POST':
user_form = UserForm(request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
else:
print (user_form.errors)
else:
user_form = UserForm()
# Render the template depending on the context.
return render_to_response(
'app/register.html',
{'user_form': user_form, 'registered': registered},
context)
这是我的urls.py:
`enter code here`enter code herefrom datetime import datetime
from django.conf.urls import url
import django.contrib.auth.views
from django.contrib.auth import views as auth_views
import app.forms
import app.views
from app.views import *
from django.views.generic import DetailView
# Uncomment the next lines to enable the admin:
from django.conf.urls import include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
# Examples:
url(r'^admin/', include(admin.site.urls)),
url(r'^$', app.views.home, name='home'),
url(r'^contact$', app.views.contact, name='contact'),
url(r'^about', app.views.about, name='about'),
url(r'^MMIR/$', app.views.MMIR, name='MMIR'),
url(r'^New_Order/$', app.views.New_Order, name='New_Order'),
url(r'^register/$', app.views.register, name='register'),
url(r'^MMIR/(?P<pk>\d+)/$', order_details.as_view(), name='order_details'),
url(r'^login/$',
django.contrib.auth.views.login,
{
'template_name': 'app/login.html',
'authentication_form': app.forms.BootstrapAuthenticationForm,
'extra_context':
{
'title': 'Log in',
'year': datetime.now().year,
}
},
name='login'),
url(r'^logout$',
django.contrib.auth.views.logout,
{
'next_page': '/',
},
name='logout'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
]
以下是我得到的实际错误:
NoReverseMatch at /
Reverse for 'auth_logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://localhost:61568/
Django Version: 1.10.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'auth_logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable: C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\Scripts\python.exe
Python Version: 3.5.2
Python Path:
['C:\\Users\\USER\\Documents\\Visual Studio '
'2015\\Projects\\DjangoWebProject2\\DjangoWebProject2',
'C:\\Users\\USER\\Documents\\Visual Studio '
'2015\\Projects\\DjangoWebProject2\\DjangoWebProject2\\env\\lib\\site-packages\\django_registration_redux-1.4-py3.5.egg',
'C:\\Users\\USER\\Documents\\Visual Studio '
'2015\\Projects\\DjangoWebProject2\\DjangoWebProject2\\env\\Scripts\\python35.zip',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python35-32',
'C:\\Users\\USER\\Documents\\Visual Studio '
'2015\\Projects\\DjangoWebProject2\\DjangoWebProject2\\env',
'C:\\Users\\USER\\Documents\\Visual Studio '
'2015\\Projects\\DjangoWebProject2\\DjangoWebProject2\\env\\lib\\site-packages']
Server time: Tue, 6 Dec 2016 18:05:25 -0600
Error during template rendering
In template C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\app\templates\app\index.html, error at line 0
Reverse for 'auth_logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
1 {% extends "app/layout.html" %}
2 {% block content %}
3
4 <div class="jumbotron">
5 <h1>LMMS Web Portal</h1>
6 <p class="lead">Vendor portal for entering shipping info</p>
7 <a href="{% url 'register' %}>Register Here</a>
8 </div>
9
10 {% endblock %}
Traceback Switch to copy-and-paste view
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\core\handlers\base.py in _legacy_get_response
response = self._get_response(request) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\app\views.py in home
'year':datetime.now().year, ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\shortcuts.py in render
content = loader.render_to_string(template_name, context, request, using=using) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\loader.py in render_to_string
return template.render(context, request) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\backends\django.py in render
return self.template.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render
return self._render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in _render
return self.nodelist.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render
bit = node.render_annotated(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render_annotated
return self.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\loader_tags.py in render
return compiled_parent._render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in _render
return self.nodelist.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render
bit = node.render_annotated(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render_annotated
return self.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\defaulttags.py in render
return nodelist.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render
bit = node.render_annotated(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\base.py in render_annotated
return self.render(context) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\template\defaulttags.py in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\urls\base.py in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) ...
▶ Local vars
C:\Users\USER\Documents\Visual Studio 2015\Projects\DjangoWebProject2\DjangoWebProject2\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns) ...
▶ Local vars
如果您需要查看其他文件以帮助诊断问题,请告诉我,我会发布。
感谢您的帮助! 最大
答案 0 :(得分:0)
这是您的退出网址
url(r'^logout$',
django.contrib.auth.views.logout,
{
'next_page': '/',
},
name='logout'),
但它的名称只是logout
,而不是auth_logout
。所以你在urls.py中实际上没有名为auth_logout的视图。而且我确实认为对auth_logout的调用位于app/layout.html
答案 1 :(得分:0)