我有一个自定义用户模型,除了电子邮件和密码之外还包含许多字段。其中一个字段是user_type
,可以设置为设计师或开发人员,其他字段特定于这两种类型之一。
我需要为每个用户类型设置单独的注册表格。
使用Django-allauth设置一个带有自定义字段的注册表格很容易,因为我可以使用ACCOUNT_SIGNUP_FORM_CLASS
设置,但我不知道如何设置多个表格。
我有一个自定义用户模型,除了电子邮件和密码之外还包含许多字段。其中一个字段是user_type
,可以设置为设计师或开发人员,其他字段特定于这两种类型之一。
我需要为每个用户类型设置单独的注册表格。
使用Django-allauth设置一个带有自定义字段的注册表格很容易,因为我可以使用ACCOUNT_SIGNUP_FORM_CLASS
设置,但我不知道如何设置多个表格。
虽然距离上次回答已经很久了,但希望这能对某些人有所帮助。用户被扩展为具有“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/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>
ACCOUNT_FORMS = {'signup': 'customauth.forms.AgenttSignUpForm'}
,否则保存函数不会运行? - cristian看起来你需要将它们放在不同的模型中。
为这些注册设计单独的视图和URL,你的视图可能会像这样:
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))
以及你的表单
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 = ()
我相信你能够解决这个问题。
祝你好运!