问题
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:
- Emits the
user_signed_up
signal - 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:
- Extend the SignupView from
allauth/account/views.py
and override itsform_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.
- 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