我已经搜索了类似的问题和答案。我希望通过 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/