2017-07-28 79 views
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错误。

我想知道原因和解决办法。

回答

1

我不确定这是否会有所帮助。如果它没有看到错误的回溯可能会有所帮助。

尝试改变

LOGIN_URL = 'accounts/login' 

LOGIN_URL = '/accounts/login/' 
+0

谢谢,克里斯!它现在有效。 –

相关问题