I'm trying to learn django by following the book "Django by Example" and probably due to conflicting versions i'm running into this problem when trying to use django.auth together with some URL settings in settings.py. Im getting totally frustrated by now since i have no idea how to even begin debugging this error. Any help or advice would be much appreciated Here's the relevant part of the settings.py file
我正试图通过以“django by Example”的方式来学习django,可能是因为在使用django时遇到了冲突的版本。在setter .py中连同一些URL设置一起使用。我现在非常沮丧,因为我甚至不知道如何开始调试这个错误。如果您有任何帮助或建议,我将非常感谢。py文件
from django.core.urlresolvers import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')
app views.py:
应用views.py:
from django.shortcuts import render, redirect
from django.shortcuts import HttpResponse
from django.contrib.auth import authenticate, login, logout
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
urls.py
urls . py
from django.conf.urls import url
from . import views
app_name = 'account'
urlpatterns = {
url(r'^$', views.dashboard, name='dashboard'),
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'),
}
Main urls.py :
主要的url。py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/', include('account.urls')),
]
错误消息
updated settings.py :
更新设置。py:
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
1 个解决方案
#1
1
When you use app_name
that sets up a namespace that will be used when you include()
that urls.py somewhere else.
当您使用app_name设置名称空间时,当您包含()url时将使用该名称空间。py别处。
So there's no url with the name "login"
, instead it's called "account:login"
, and that's the name you have to pass to reverse().
因此,这里没有名为“login”的url,而是称为“account:login”,这是您必须传递给reverse()的名称。
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
Relevant docs: URL namespaces and included URLconfs
相关文档:URL名称空间和包含URLconfs
If you are using django-extensions (you should), you can use the management command show_urls
to get a nicely formatted list of all the url routes that are registered in your project.
如果您正在使用django扩展(您应该使用),您可以使用管理命令show_urls来获得一个格式良好的列表,其中包含您项目中注册的所有url路由。
#1
1
When you use app_name
that sets up a namespace that will be used when you include()
that urls.py somewhere else.
当您使用app_name设置名称空间时,当您包含()url时将使用该名称空间。py别处。
So there's no url with the name "login"
, instead it's called "account:login"
, and that's the name you have to pass to reverse().
因此,这里没有名为“login”的url,而是称为“account:login”,这是您必须传递给reverse()的名称。
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
Relevant docs: URL namespaces and included URLconfs
相关文档:URL名称空间和包含URLconfs
If you are using django-extensions (you should), you can use the management command show_urls
to get a nicely formatted list of all the url routes that are registered in your project.
如果您正在使用django扩展(您应该使用),您可以使用管理命令show_urls来获得一个格式良好的列表,其中包含您项目中注册的所有url路由。