Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple signup pages with django-allauth?

I have one custom user model that contains a number of fields in addition to email and password. One field is user_type which is set to either designer or developer. Other fields are specific to one or the other type.

I need to have a separate signup form for each user type.

Setting up one signup form with custom fields was easy with django-allauth as I could make use of the ACCOUNT_SIGNUP_FORM_CLASS setting. I'm not sure how to setup more than one.

like image 934
KrisF Avatar asked Sep 30 '13 02:09

KrisF


1 Answers

It has been a long time after the last answer is given but hope that this will help someone. User is extended with column "account_type".

forms.py

from django import forms

from allauth.account.forms import SignupForm


class AgentSignUpForm(SignupForm):

    first_name = forms.CharField(max_length=30, label='First name', required=False)
    last_name = forms.CharField(max_length=30, label='Last name', required=False)

    def save(self, request):
        user = super(AgentSignUpForm, self).save(request)
        user.account_type = 1
        user.save()
        return user

class CandidateSignUpForm(SignupForm):

    first_name = forms.CharField(max_length=30, label='First name', required=False)    
    last_name = forms.CharField(max_length=30, label='Last name', required=False)

    def save(self, request):
        user = super(CandidateSignUpForm, self).save(request)
        user.account_type = 2
        user.save()
        return user

views.py

from django.shortcuts import render

from allauth.account.views import SignupView

from .forms import AgentSignUpForm
from .forms import CandidateSignUpForm


class AgentSignUp(SignupView):

    template_name = 'allauth/signup_agent.html'
    form_class = AgentSignUpForm
    redirect_field_name = 'next'
    view_name = 'agent_sign_up'

    def get_context_data(self, **kwargs):
        ret = super(AgentSignUp, self).get_context_data(**kwargs)
        ret.update(self.kwargs)
        return ret

class CandidateSignUp(SignupView):

    template_name = 'allauth/signup_candidate.html'
    form_class = CandidateSignUpForm
    redirect_field_name = 'next'
    view_name = 'candidate_sign_up'

    def get_context_data(self, **kwargs):
        ret = super(CandidateSignUp, self).get_context_data(**kwargs)
        ret.update(self.kwargs)
        return ret

urls.py

from django.conf.urls import url, include

from . import views

urlpatterns = [
    url(r'^agent-sign-up/', views.AgentSignUp.as_view(), name='agent-sign-up'),
    url(r'^candidate-sign-up/', views.CandidateSignUp.as_view(), name='candidate-sign-up'),
]

2 templates

#templates/allauth/signup_agent.html
<form method="post" action="{% url 'agent-sign-up' %}">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="ok" />
</form>

#templates/allauth/signup_candidate.html
<form method="post" action="{% url 'candidate-sign-up' %}">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="ok" />
</form>
like image 174
Admir Avatar answered Nov 02 '22 14:11

Admir