重定向使用的django邀请链接

时间:2017-10-05 12:17:28

标签: python django

我正在尝试更新现有的Django应用程序的登录过程,因为我对邀请链接存在问题。如果我发送邀请链接,它会按预期工作,并将用户通过注册表单并进入网站,但如果他们稍后尝试使用该邀请链接再次访问该网站,则会出现500错误({{1} })。我想将所有过期的邀请链接重定向到主登录页面,以便他们只需登录而不是转到500错误页面。

我正在使用Django 1.11和邀请后端(http://django-organizations.readthedocs.io/en/latest/reference/backends.html

我看到它有一个get_success_url我可以用来将原始登录发送到主页,但是有一个get_fail_url可以用来将过期的邀请链接发送到主登录页面吗?或者有更好的方法来解决这个问题吗?

以下是我发送所有/邀请/网址的CustomerInvitations课程:

DoesNotExist: User matching query does not exist.

错误似乎发生在第class CustomerInvitations(InvitationBackend): form_class = CustomUserRegistrationForm def __init__(self): super().__init__(Customer) def get_success_url(self): return "/" def invite_by_email(self, email, sender=None, request=None, **kwargs): try: user = self.user_model.objects.get(email=email) except self.user_model.DoesNotExist: user = self.user_model.objects.create(username=self.get_username(), email=email, password=self.user_model.objects.make_random_password()) user.is_active = False user.save() self.send_invitation(user, sender, **kwargs) return user def activate_view(self, request, user_id, token): organization_ids = CustomerUser.objects.filter(user_id=user_id).all().values_list("organization_id", flat=True) organization_names = Customer.objects.filter(id__in=organization_ids).all().values_list("name", flat=True) organization_names = list(organization_names) if len(organization_names) <= 2: names = " and ".join(organization_names) else: organization_names[-1] = "and " + organization_names[-1] names = ", ".join(organization_names) user = self.user_model.objects.get(id=user_id, is_active=False) form = self.get_form(data=request.POST or None, instance=user) if len(organization_names) and not form.is_valid(): messages.info(request, "Join "+names+" on the website!") return super().activate_view(request, user_id, token) 行,所以我考虑使用try-except语句来捕获错误,但我更倾向于使用更清晰,更易理解的解决方案,因为我不会维护此代码。< / p>

请原谅我,如果解决方案显而易见,我将失踪。我刚刚在一周前开始使用Django,这是我对Web应用程序的第一次介绍。

1 个答案:

答案 0 :(得分:0)

您可以尝试删除is_active = False并添加重定向:

user = self.user_model.objects.get(id=user_id)
if user.is_active:
     # Change your login url name
     return redirect('login')