フォームを記入して送信すると、データベースに何も追加されず、空のパスワードフィールドを含むページがリロードされます。また、フォームにVerifypassword ValidationErrorと等しくないパスワードが含まれていても表示されません。 ... Uに感謝データベースにユーザを追加できません
forms.py
class SignUpForm(ModelForm):
username = forms.CharField(label=(u'Username'))
first_name = forms.CharField(label=(u'First name'))
last_name = forms.CharField(label=(u'Last name'))
address = forms.CharField(label=(u'Address'))
email = forms.EmailField(label=(u'Email Address'))
password = forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False))
password1 = forms.CharField(label=(u'Verify Password'),widget=forms.PasswordInput(render_value=False))
mobile = forms.IntegerField(label=(u'Mobile'))
class Meta:
model = Employer
## check whats Exclude !!!!
exclude = ('user',)
def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError("That username already exists. Please select another")
def clean(self):
if self.cleaned_data['password'] != self.cleaned_data['password1']:
raise forms.ValidationError("not matched")
return self.cleaned_data
view.py
def EmployerRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
employer =Employer.objects.create(username=form.cleaned_data["username"],password= form.cleaned_data['password'],first_name=form.cleaned_data['first_name'],last_name=form.cleaned_data['last_name'],email=form.cleaned_data['email'],address=form.cleaned_data['address'],mobile=form.cleaned_data['mobile'])
employer.save()
return HttpResponseRedirect('/login/')
else:
return render_to_response('Sign_up_Employer.html',{'form':form}, context_instance=RequestContext(request))
else:
#user is not submitting show them the registeration form
form= SignUpForm()
context = {'form':form}
return render_to_response('Sign_up_Employer.html',context,context_instance=RequestContext(request))
Sign_Up_Employer.html
{%block content%}
<form action ="" method ="post">
{% csrf_token %}
{% if form.errors %}<p>Please Correct </p> {% endif %}
<div class ="register_div">
{%if form.username.errors%}<p class= "error">{{form.username.errors}}</p>{% endif %}
<p> <label for ="username"{% if form.username.errors %} class= "error"{% endif %}> Username</label></p>
<p>{{form.username}}</p>
</div>
<div class ="register_div">
{%if form.first_name.errors%}<p class= "error">{{form.first_name.errors}}</p>{% endif %}
<p> <label for ="first_name"{% if form.first_name.errors %} class= "error"{%endif%}>First name</label></p>
<p>{{form.first_name}}</p>
</div>
<div class ="register_div">
{%if form.last_name.errors%}<p class= "error">{{form.last_name.errors}}</p>{% endif %}
<p> <label for ="last_name"{% if form.last_name.errors %} class= "error"{%endif%}>Last name</label></p>
<p>{{form.last_name}}</p>
</div>
<div class ="register_div">
{%if form.address.errors%}<p class= "error">{{form.address.errors}}</p>{% endif %}
<p> <label for ="address"{% if form.address.errors %} class= "error"{%endif%}>Address'</label></p>
<p>{{form.address}}</p>
</div>
<div class ="register_div">
{%if form.email.errors%}<p class= "error">{{form.email.errors}}</p>{% endif %}
<p> <label for ="email"{% if form.email.errors %} class= "error"{%endif%}>Email Address</label></p>
<p>{{form.email}}</p>
</div>
<div class ="register_div">
{%if form.password.errors%}<p class= "error">{{form.password.errors}}</p>{% endif %}
<p> <label for ="password"{% if form.password.errors %} class= "error"{%endif%}>Password</label></p>
<p>{{form.password}}</p>
</div>
<div class ="register_div">
{%if form.password1.errors%}<p class= "error">{{form.password1.errors}}</p>{% endif %}
<p> <label for ="password1"{% if form.password1.errors %} class= "error"{%endif%}>Verify Password</label></p>
<p>{{form.password1}}</p>
</div>
<div class ="register_div">
{%if form.mobile.errors%}<p class= "error">{{form.mobile.errors}}</p>{% endif %}
<p> <label for ="mobile"{% if form.mobile.errors %} class= "error"{%endif%}>Mobile</label></p>
<p>{{form.mobile}}</p>
</div>
<p><input type =submit alt =register></p>
</form>
{% endblock %}
しかし、(a)すべてのフィールドを明示的に定義し、(b)単に 'form.save()'を実行するのではなく、cleaned_dataからオブジェクトを手動で作成することでmodelformsのすべての点を無視しています。 。 –
私たちはhtmlテンプレートを見ることができますか? –
私はhtmlテンプレートを追加しました – May