django - 为什么 Django 在使用 @login_required 装饰器时有太多登录重定向?

标签 django authentication django-allauth

我已经搜索了类似的问题和答案。我希望通过 django-allauth View 进行身份验证后显示主页。但是为什么在使用 @login_required 装饰器时我有太多登录重定向?”请解释一下重定向循环的原因和解决方案?重定向循环的代码是HTTP/1.1''302。

我从 Django 文档中查找到

@login_required decorator does the following:

  • 如果用户未登录,则重定向到/accounts/login/,传递 查询字符串中当前的绝对 URL 作为下一个,对于 例如:/accounts/login/?next=/polls/3/。
  • 如果用户已登录,则正常执行 View 。风景 然后代码可以假设用户已登录。

我希望经过身份验证的用户进入主页,同时将未登录的用户重定向到帐户/登录页面。

我的settings.py的一部分:

LOGIN_URL='/accounts/login/'
LOGIN_REDIRECT_URL='/'

我的观点.py:

from django.shortcuts import render
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def home(request):
    context={}
    template='home.html'
    return render(request, template,context)

我的 urls.py :

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin   
from profiles import views as profiles_views

urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'$',profiles_views.home,name='home'),
url(r'^accounts/',include('allauth.urls')),

最佳答案

问题出在你的 urls.py 中。

您没有放置标记行开头的“^”

尝试,

urlpatterns=[
    url(r'^admin/',admin.site.urls),
    url(r'^$',profiles_views.home,name='home'),
    url(r'^accounts/',include('allauth.urls')),
]

关于django - 为什么 Django 在使用 @login_required 装饰器时有太多登录重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095964/

相关文章:

Django 1.8.5 allauth 和项目应用程序名称冲突

Django get() 缺少 1 个必需的位置参数 : 'header'

python - 提交表单时出现 Django IntegrityError (在 View 中预填充)

python - 两个具有相同结构的 URL

java - BasicTomcat 身份验证失败

authentication - Silex 应用程序中的自定义身份验证提供程序

python - Django-ManyToMany表提取了错误的信息

javascript - 如何使用一个 JWT token 签署第二个 JWT token ?

python - 我无法让 OpenID 工作(openid.consumer.consumer.ProtocolError : Parameter next not in return_to URL)

javascript - 向 google 注册的链接在我的页面上给出错误,但在 allauth 页面上没有给出错误