gpt4 book ai didi

django - 如何正确自定义 Django LoginView

转载 作者:行者123 更新时间:2023-12-02 08:31:01 28 4
gpt4 key购买 nike

我试图弄清楚如何根据用户当天是否第一次登录来自定义 django LoginView。我当前已设置 LoginView,使其默认为 settings.py 文件中的 LOGIN_REDIRECT_URL = "book:author"。这工作完美无缺。当用户登录并成功通过身份验证时,他们将被重定向到“book:author”,正如我所期望的那样。

我想做的是,如果这是用户当天第一次登录,则将他们定向到一个 URL,如果是当天的任何其他登录迭代,则将他们重定向到另一个 URL。我已经阅读了有关如何执行此操作的各种方法,使用消息传递而不是条件 URL 重定向来使用 NEXT 参数,并且我试图找出哪种方法是最好、最安全和正确的方法。

这是我的默认登录 View ...(没什么花哨的)

class LoginView(LoginView):
template_name = 'registration/login.html'
form_class = AuthenticationForm

然后它根据我的 settings.py 文件定义进行重定向...

    LOGIN_REDIRECT_URL = "book:author" 

将当天首次登录重定向到不同 URL 的最佳方式是什么?

预先感谢您的任何建议。

我找到了这个答案Django -- Conditional Login Redirect这似乎就是我正在寻找的。使用底部的示例有什么缺点吗?

与基于函数的示例相比,如何使用 LoginView?

最佳答案

为了回答您的问题,让我假设您有一个客户端模型,在某种程度上,与此模型类似,我们需要一个存储用户登录信息的辅助模型:

models.py:

from django.db import models
from django.contrib.auth.models import User

class Client(models.Model):
"""
This client model is pointing to django's User model
You can use your custom user model using AbstractBaseUser or whatever.
And if you're using django's User model directly, this class is not required
"""
user = models.OneToOneField(
User,
on_delete=models.DO_NOTHING,
verbose_name='User',
related_name='cli', # related name manager if needed
null=False,
blank=False,
)

def __str__(self):
return '{}'.format(self.user.username)


class ClientLogins(models.Model):
"""
This is a helper model table that stores the logins dates of the client
"""
client = models.ForeignKey(
Client,
verbose_name='Client',
on_delete=models.DO_NOTHING
)
date = models.DateTimeField(verbose_name="Date login")

def __str__(self):
return '{}'.format(self.client)

然后你的表格:

forms.py:

class LoginForm(forms.ModelForm):
'''Simple login form'''
class Meta:
model = User
fields = ('username', 'password')

最后,您的登录行为应该在 View 类/函数中处理。

views.py:

from datetime import timedelta
from django.utils import timezone
from MY_APP import models, forms

class LoginUser(LoginView):
template_name = 'login.html' # your template
from_class = forms.LoginForm # your form

def get_success_url(self):
'''Here the part where you can implement your login logic'''
now = timezone.now()
# Get current day date object
# like: 12/02/2019 00:00:00
today = now.replace(minute=0).replace(second=0).replace(microsecond=0)
# Get the client from the user object
client = self.request.user.cli
# Get all the user today's logins and count them
client_logins = models.ClientLogins.objects.filter(
client=client,
date__gte=today,
date__lte=today + timedelta(days=1)
).count()
if client_logins < 1: # Or: if not client_logins:
# create a login tracker record
models.ClientLogins.objects.create(
client=client,
date=now # Store the date where the user logged in the website
)
return reverse_lazy('FIRST_LOGIN_REDIRECT_URL')
# Or redirect to: settings.LOGIN_REDIRECT_URL
return super().get_success_url()

更多信息,这是 LoginView 的核心代码,这样您就可以知道 MRO 列表以及可以覆盖哪些内容以获得所需的行为。

关于django - 如何正确自定义 Django LoginView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com