我希望节省用户的上次注销时间。我的想法是将其添加到用户个人资料模型中。我正在使用Django 1.11.15
示例:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
...
last_logout = models.DateTimeField(blank=True, null=True)
下面是我如何创建个人资料模型以供参考
def signup(request):
if request.method == 'POST':
form = UserCreateForm(request.POST or None)
if form.is_valid():
new_user = form.save()
Profile.objects.create(user=new_user)
return redirect('accounts:edit_profile')
else:
form = UserCreateForm()
context = {'form': form}
return render(request, 'accounts/signup.html', context)
以下是我打算做的。这是正确的方法吗?我想添加到django的默认注销/注销方法中,我不确定它的名称是signout
还是logout
class LoggedOut(TemplateView):
template_name = 'logged_out.html'
def signout(self):
"""logout user """
self.request.user.profile.last_logout = datetime.now()
self.request.user.profile.save()
我的网址
url(r'^loggedout/$', views.LoggedOut.as_view(), name='loggedout'),
答案 0 :(得分:2)
您可以将Django logout信号用于此目的。
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver
@receiver(user_logged_out)
def sig_user_logged_out(sender, user, request, **kwargs):
user.profile.last_logout = datetime.now()
user.profile.save()