我试图编写一个中间件来重定向auth用户,如果他们是无效的成员。
这是我的代码:
from django.conf import settings
from django.http import HttpResponsePermanentRedirect
class UserCheckMiddleware:
def process_request(self, request):
#Paths
inactive_path = settings.INACTIVE_USER_URL
#First check if the user is logged in
if request.user.is_authenticated():
#Check if the user has a valid membership
if not request.user.profile.is_valid_member:
#Check if the path is the inactive_path to prevent endless redirectloop
if not request.path in (inactive_path):
#Redirect to inactive_path
return HttpResponsePermanentRedirect(inactive_path)
这是我的型号代码:
class Profile(models.Model):
user = models.OneToOneField(User)
memberend = models.DateTimeField(blank=True, null=True)
def is_valid_member(self):
if self.memberend:
#Return True if memberend is in the future and False if the date is in the past
return self.memberend >= timezone.now()
else:
#Memberend is not set, the user is valid
return True
当我将memberend日期设置为过去的日期时,任何人都知道为什么我没有被重定向?
答案 0 :(得分:1)
您还没有调用该方法。
if not request.user.profile.is_valid_member():
方法对象本身始终为True。