如何解决Django Localhost重定向次数过多
您好,尽管我使用django allauth处理我的身份验证,但我创建了一个装饰器来保护某些视图免遭未经授权的人员的侵害,因此我使用了它的登录URL。在测试授权是否有效而不是将我重定向到登录页面时,Localhost陷入重定向循环 这是我的代码
decorators.py
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test
def seller_required(function=None,redirect_field_name=REDIRECT_FIELD_NAME,login_url='account_login'):
'''
Decorator for views that checks that the logged in user is a seller,redirects to the log-in page if necessary.
'''
actual_decorator = user_passes_test(
lambda u: u.is_active and u.is_seller,login_url=login_url,redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
views.py
from django.views.generic import CreateView,DetailView,ListView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from .decorators import seller_required
@method_decorator( seller_required,name='dispatch')
class SellerDashBoardView(ListView):
model = Seller
template_name = 'seller_dashboard.html'
应用级网址
from django.urls import path
from .views import SellerSignUpView,SellerDashBoardView
urlpatterns = [
path('seller_reg/',SellerSignUpView.as_view(),name='seller_reg'),path('seller/',SellerDashBoardView.as_view(),name='seller_dash')
]
项目级网址
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/',admin.site.urls),path('users/',include('user.urls')),#path('users/',include('django.contrib.auth.urls')),path('accounts/',include('allauth.urls')),path('',include('pages.urls')),path('store/',include('store.urls')),#path("djangorave/",include("djangorave.urls",namespace="djangorave")),] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。