Django Allauth Signup Prevent Login

偶尔善良 提交于 2021-01-28 04:26:04

问题


Is there anyway to prevent the Allauth Signup form from automatically logging in the User?

I found a similar post here with no answers:

prevent user login after registration using django-allauth

Thank you.


回答1:


You get logged in because this behavior is baked into the signup view of the allauth. When the form is valid, the view calls the function called complete_signup that does two things:

  1. Emits the user_signed_up signal
  2. Logs a user in

To solve this, we need to leave the step 1 and replace the step 2 with a simple redirect.

Here's how this can be done:

  1. Extend the SignupView from allauth/account/views.py and override its form_valid method like this:

class CustomSignupView(SignupView): def form_valid(self, form): # By assigning the User to a property on the view, we allow subclasses # of SignupView to access the newly created User instance self.user = form.save(self.request) try: signals.user_signed_up.send( sender=self.user.__class__, request=self.request, user=self.user, **{} ) return HttpResponseRedirect(self.get_success_url()) except ImmediateHttpResponse as e: return e.response

Haven't tested the code, but it should be working.

  1. Wire your new view up in urls.py, so it replaces the old Signup view url.



回答2:


I am using django-allauth 0.42.0

There is no need to extend the SignUp view, it can be achieved by the setting:

ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

in your project settings.py file.



来源:https://stackoverflow.com/questions/50893917/django-allauth-signup-prevent-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!