1
我刚刚开始学习Django 1.10。在这样做的时候,我想用all-auth
包和login_required
装饰器来实现验证功能。我写的urls.py
文件的片段代码。在使用allauth的Django 1.10认证中,login_required修饰器不起作用
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from profiles import views as profiles_views
from contact import views as contact_views
urlpatterns = [
....
url(r'^profile/$', profiles_views.userProfile, name='profile'),
url(r'^accounts/', include('allauth.urls')),
]
这是views.py
文件的代码。
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
....
@login_required
def userProfile(request):
user = request.user
context = {'user' : user}
template = "profile.html"
return render(request,template,context)
这里是设置为allauth
。
LOGIN_URL = 'accounts/login'
LOGIN_REDIRECT_URL = '/'
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_CONFIRM_EMAIL_ON_GET = False
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = LOGIN_URL
ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = None
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 3
ACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = None
ACCOUNT_EMAIL_SUBJECT_PREFIX = "My subject : "
ACCOUNT_DEFAULT_HTTP_PROTOCOL = "http"
ACCOUNT_LOGOUT_ON_GET = False
ACCOUNT_LOGOUT_REDIRECT_URL = '/'
ACCOUNT_SIGNUP_FORM_CLASS = None
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = "username"
ACCOUNT_USER_MODEL_EMAIL_FIELD = "email"
ACCOUNT_USERNAME_MIN_LENGTH = 5
ACCOUNT_USERNAME_BLACKLIST = []
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_PASSWORD_INPUT_RENDER_VALUE = False
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
正如你所看到的,我用login_required
装饰来userProfile
功能。所以如果我在localhost/profile
的浏览器上没有登录,浏览器应该被重定向到localhost/accounts/login
- 到Login
页面。
但是每当我去localhost/profile
,浏览器重定向到http://localhost:8000/profile/accounts/login?next=/profile/
,当然我得到Page not found
错误。
我想知道原因和解决办法。
谢谢,克里斯!它现在有效。 –