简体   繁体   中英

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.

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>

Looks to me you nee to have these on seperate models.

Design seperate views and urls for these signup, your view may look something like this

def register_account(request):
    template = 'shopper/register.html'
    if request.POST:
        form = UserprofileForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            password1 = form.cleaned_data['password1']
            u = User(username=username, email=email, last_name=last_name, first_name=first_name)
            u.set_password(password1)
            u.save()
    else:
        form = UserprofileForm()
    return render_to_response(template,
                          {'form': form}, context_instance=RequestContext(request))

and your form

class UserprofileForm(ModelForm):

required_css_class = 'required'

username = forms.RegexField(regex=r'^[\w.@+-]+$',
                            max_length=30,
                            label=_("Username"),
                            error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
def clean_username(self):
    """
    Validate that the username is alphanumeric and is not already
    in use.

    """
    existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
    if existing.exists():
        raise forms.ValidationError(_("A user with that username already exists."))
    else:
        return self.cleaned_data['username']


password1 = forms.CharField('Password', widget=forms.PasswordInput(), help_text='Password')
password2 = forms.CharField('Repeat Password', widget=forms.PasswordInput(), help_text='Repeat Password')

def clean_password2(self):
    password1 = self.cleaned_data.get('password1')
    password2 = self.cleaned_data.get('password2')
    if not password1:
        raise forms.ValidationError("You must confirm your password")
    if password1 != password2:
        raise forms.ValidationError("Your passwords do not match")
    return password2
#town = forms.ModelChoiceField(queryset=Town.objects.all(), widget=forms.Select(attrs={'style': 'width: 100%;', 'data-placeholder': 'Select Town', 'tabindex': '2'}))
class Meta:
  model = Userprofile
  exclude = ()

From there am sure you will be able to workout the ret.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM