-1

I am trying to create different django-allauth signup forms for different types of users. So different forms for different user types. Once a user selects a user type (example: "I want to sign up as a vendor"), they should be redirected to a different form where they will complete other signup and profile details before email confirmation and login. I have one django-allauth signup form working, but not multiple. Currently I have a general purpose signup form assigned to users/accounts/signup/ but I also want a vendor specific signup form assigned to users/accounts/signup_vendor/. I have tried everything I can think of.

The best possible solution I've found, while not ideal, is provided by the django-allauth author, stating:

"There are multiple ways to approach this. You could store the desired user type into the session, and dynamically construct your form fields depending on that. Or, you could allow for the user to make up his mind at any time by simply adding a "profile type" to the sign up form. Basically, the fields in the signup form would have to be the union of all possible fields, and perhaps you would want to add some Javascript to dynamically show/hide some fields depending on the type chosen. I would recommend an approach where the user can make up his mind about the profile type at any stage." link to thread

I am new to django, and new to web app dev. How can I best store user type data into sessions, and then dynamically construct form fields depending on that?

Currently, this is what I have: settings.py

#assign to custom form
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.GeneralSignupForm'

users.models.py

    class User(AbstractUser):
        class UserTypes(models.TextChoices):
            ....
        ....
    class VendorUser(models.Model):
        userinfo = models.OneToOneField(User, on_delete=models.CASCADE)

users.forms.py

class GeneralSignupForm(forms.Form):
    first_name = forms.CharField(max_length=255, help_text="Your first name")
    last_name = forms.CharField(max_length=150, help_text="Your last name")
    phone = forms.CharField(max_length=150)

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.phone = self.cleaned_data['phone']
        user.save()

users.views.py

class GeneralSignupView(SignupView):
    template_name = 'account/signup.html'
    form_class = GeneralSignupForm
    redirect_field_name = 'next'
    view_name = 'signup'
    success_url = None

    def get_context_data(self, **kwargs):
        data = super(GeneralSignupView, self).get_context_data(**kwargs)
        data.update(self.kwargs)
        return data

users.urls.py

path('accounts/signup/', views.GeneralSignupView.as_view(), name='signup'),

urls.py

    path('users/', include('users.urls')),
    url(r'^accounts/', include('allauth.urls')),
Zuckerbrenner
  • 323
  • 2
  • 15

1 Answers1

0

what about creating a model and linking it to user model with ForeignKey? for instance, you can name your model 'type', than use it with user.type

  • Thanks for your suggestion. Yes, I already have that model structure actually (I forgot to include that in my description). My issue is how best to dynamically render a form specific to user.type within the constraints of django-allauth. – Zuckerbrenner Feb 19 '21 at 20:02