这里每次输入任何数据都没有发生任何事情,除了重新加载页面,密码和验证密码是空的,即使我没有写它们通常相同的raise.ValidationError没有显示任何建议吗?
Views.py
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from shoghlanah.forms import SignUpForm
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from shoghlanah.models import *
def EmployerRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user=User.objects.create_user(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'])
user.save()
employer =Employer(user=user,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()
#Employer=user.get_profile()
#Employer.first_name=form.cleaned_data['first_name']
#Employer.last_name=form.cleaned_data['last_name']
#Employer.address=form.cleaned_data['address']
#Employer.mobile =form.cleaned_data['mobile']
#Employer.save()
return HttpResponseRedirect('/login/')
#else:
# return render_to_response('Sign_up_Employer.html',{'form':form},context,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))
forms.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.forms import ModelForm
from shoghlanah.models import *
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
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
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 %}
models.py
from django.db import models
class Employer(models.Model):#Employer inherits from User
user = models.OneToOneField(User)
employer_verified = models.BooleanField(default=False)
profile_picture = models.ImageField(upload_to='ProfilePictureDB', null=False , blank=False)
#profile pictures will be uploaded to ProfilePictureDB
hidden_values = models.CharField(max_length=200)
#hidden_values will contain all the attributes that the employer/contractor wants to hide
signature = models.OneToOneField(Signature)
mobile = models.IntegerField(max_length=20)
address = models.CharField(max_length=100)
def __unicode__(self):
return self.name
答案 0 :(得分:1)
if request.method == "POST":
if form.is_valid():
# ....
return HttpResponseRedirect('/login/') # not indented
返回是从is_valid()块中取消缩进的。因此,无论表单的有效性如何,POST都将重定向。改为
if request.method == "POST":
if form.is_valid():
# ....
return HttpResponseRedirect('/login/') # indented
更新
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")
这是错的;你回来后不能抚养。加注应该在try块中。
def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
raise forms.ValidationError("That username already exists. "
"Please select another")
except User.DoesNotExist:
return username