我希望用户必须在首次登录后填写表单。有了我已经尝试过的信号:
# -*- coding: utf-8 -*-
from django.contrib.auth.signals import user_logged_in
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
[...]
def umleiten(sender, user, request, **kwargs):
print reverse("first_login")
"""Check if a user logged on for the first time."""
return HttpResponseRedirect(reverse("first_login"))
user_logged_in.connect(umleiten)
调用函数“umleiten”但不会将其转发到相应的页面。 有没有办法在登录后调用函数,我可以从中转发到另一个站点。
答案 0 :(得分:0)
您可以并且可能应该使用中间件
class FirstLoginCheckMiddleware(object):
def process_request(self, request):
is_form_page = request.path in (
settings.FORM_PAGE
)
if (not is_form_page) and request.user.is_authenticated() and (not request.user.profile.filled_out_the_form):
return HttpResponseRedirect(settings.FORM_PAGE)
return None
例如,你用一个具有BooleanField的对象扩展了User,一旦用户做了你想要的东西,就会检查它:
class UserProfile(models.Model):
filled_out_the_form = field.BooleanField(default=False)
# ...