Search code examples
djangodjango-viewsdjango-allauth

Django - Getting contact permission from users


On my sign up forms, I am asking users if I can contact them via email or SMS.

I am using Django-Allauth and it doesn't seem to list it as an option. I am subclassing the view and trying to sneak it in there:

class ListenSignupView(SignupView):
    template_name = 'listen_signup.html'

    def form_valid(self, form):
        form.allows_contact = self.request.POST.get('allows_contact')
        return super(ListenSignupView, self).form_valid(form)

However, it doesn't actually save the result to the user. Do I also need to subclass the forms? What is the easiest way to accomplish this?


Solution

  • As it turns out, overriding the forms is pretty simple:

    from allauth.account.forms import SignupForm
    
    
    class CustomSignupForm(SignupForm):
    
        def save(self, request):
            user = super(CustomSignupForm, self).save(request)
            user_input = request.POST.get('allows_contact')
            user.allows_contact = True if user_input == "on" else False
            user.save()
            return user
    

    This can be done for any of allauth's forms. Here are the relevant docs, for reference.