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
錯誤。
我想知道原因和解決辦法。
謝謝,克里斯!它現在有效。 –