我使用django Allauth进行用户注册和登录。 我为不同类型的用户提供了两种注册表单。
Setting.py
ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.mySignupForm_1'
form.py
class mySignupForm_1(forms.Form):
def signup(self, request, user):
user.save()
class mySignupForm_2(forms.Form):
email = forms.EmailField(max_length=255, label="Email")
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Password (again)")
last_name = forms.CharField(max_length=60, label='Last Name')
first_name = forms.CharField(max_length=60, label='First Name')
print('1111') # it prints when the template is render.
def signup(self, request, user):
print('22222') # this line never print. It seems the program never reach here
# last_name and first_name are not saved in the Database. WHY?
user.email = self.clean_data['email']
user.last_name = self.clean_data['last_name']
user.first_name = self.clean_data['first_name']
user.save()
view.py
class JointLoginSignupView(LoginView):
form_class = LoginForm
signup_form = SignupForm
def __init__(self, **kwargs):
super(JointLoginSignupView, self).__init__(*kwargs)
def get_context_data(self, **kwargs):
ret = super(JointLoginSignupView, self).get_context_data(**kwargs)
ret['mysignupform1'] = get_form_class(app_settings.FORMS, 'signup', self.signup_form)
return ret
login = JointLoginSignupView.as_view()
class CustomSignupView2(SignupView):
template_name = "account/merchant_signup.html"
custom_signup_form2 = MerchantSignupForm
def __init__(self, **kwargs):
super(CustomSignupView2, self).__init__(*kwargs)
def get_context_data(self, **kwargs):
ret = super(CustomSignupView2, self).get_context_data(**kwargs)
ret['custom_signup_form2'] = get_form_class(app_settings.FORMS, 'signup', self.custom_signup_form2)
return ret
custom_signup_form2= CustomSignupView2.as_view()
url.py
url(r'^signup1/', 'allauth.views.signup', name='signup1'),
url(r'^signup2/', 'allauth.views.custom_signup_form2', name='signup2'),
显示错误的模板1
<form class="signup_form" name="registerForm" action="{% url 'signup1' %}" method="post">
{% csrf_token %}
{{mysignupform1| crispy }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div>
<button type="submit" class="btn btn-group btn-primary">{%trans 'Create an Account' %}</button>
</div>
</form>
模板2,不显示错误
<form class="signup_form" name="registerForm" action="{% url 'signup2' %}" method="post">
{% csrf_token %}
{{custom_signup_form2| crispy }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div>
<button type="submit" class="btn btn-group btn-primary">{%trans 'Create an Account' %}</button>
</div>
</form>
如果我在自定义signup_form1中输入了错误的数据,则会显示错误,如下所示
我有两个问题:
如果我在第二个自定义注册表单中执行相同操作,则不会显示这些错误。如何使第二个表单显示验证错误?
,似乎永远无法访问函数 mySignupForm_2 中的 print('222')行。并且不保存额外的字段姓氏和名字。我在这里做错了吗?
答案 0 :(得分:0)
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
您在设置文件中将ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.mySignupForm_1'
设置为customform1。将其设置为customform2,看看会发生什么。在这种情况下应该调用print语句。
但是,据我所知,django_allauth中无法设置多个注册表单。