在使用allauth的Django 1.10身份验证中,login_required装饰器不起作用

时间:2017-07-28 18:16:08

标签: python django

我刚开始学习Django 1.10。在这样做时,我想使用all-auth包和login_required装饰器实现身份验证功能。

以下是我撰写的urls.py文件的代码段。

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 个答案:

答案 0 :(得分:1)

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

尝试更改

LOGIN_URL = 'accounts/login'

LOGIN_URL = '/accounts/login/'