问题描述
我正在使用Django身份验证系统,并且还有一个关于用户的模型:
class user(models.Model) :
user = models.OneToOneField(User)
user_code = models.IntegerField(max_length = 3)
phone = models.IntegerField(max_length = 10)
task = models.ForeignKey (任务)
class Meta:
unique_together =('user_code','task')
def __unicode __(self):
return str(self.user_code)
所以我做了两个表单进行用户注册(form.py):
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $(((((((((((((((((((((((((((((((((( )
class Form4(forms.ModelForm):
class Meta:
model = user
现在在我看来,我想发送两个表单到模板并提交两者。一切都很好,但提交后,我无法检查表单验证?这是我的观点:
def add_new_user(request):
如果request.method =='POST':#如果表单已经提交...
user_form = userForm(request.POST)#django用户
form = Form4(request.POST)#我的模型用户
如果user_form.is_valid()和form.is_valid():#所有验证规则通过**编辑** is_valide
user_name = user_form.cleaned_data ['username']
password = user_form.cleaned_data ['password']
name = user_form.cleaned_data ['first_name']
user_id = form.cleaned_data ['user_code']
email = user_form.cleaned_data ['email']
phone = form.cleaned_data ['phone ']
task = form.cleaned_data ['task']
else:#** added ** form invalid statement
return render(request,'student / form4.html',{'形式':form,'user_form':user_form})
尝试:#**从有效的语句移动**
做somthing ...
除了ValueError:
做somthing ...
else:
form = Form4( )
user_form = userForm()
#未绑定的形式
返回渲染(请求,'student / form4.html',{'form':form,'user_form':user_form})
这是我的模板(form4.html):
< form name =myFormaction ={%url'student:add_new_user'%}onsubmit =return validateForm4()方法= POST >
{%csrf_token%}
< div id =form-data>
{{form.non_field_errors}}
{{user_form.username}} {{user_form.username.errors}}
< label for =id_user_name>用户名: /标签><峰; br>
{{user_form.password}} {{user_form.password.errors}}
< label for =id_password> password:< / label>< br>
{{user_form.first_name}} {{user_form.first_name.errors}}
< label for =id_name>名字:< / label>< br>
{{user_form.email}} {{user_form.email.errors}}
< label for =id_email>电子邮件:< / label>< br>
{{form.user_code}} {{form.user_code.errors}}
< label for =id_email>使用ID:< / label>< br>
{{form.phone}} {{form.phone.errors}}
< label for =id_phone>电话号码:< / label>< br>
{{form.task}} {{form.task.errors}}
< label for =id_task> task:< / label>< br>
< input type =submitvalue =register/>< br>
< / div>
< / form>
但是对于验证都返回False。如何检查验证?
ANSWER :
我添加哪个字段想从用户和用户。并将我的form.py更改为:
class userForm(forms.ModelForm):
class Meta:
model = User
fields =('username','password','first_name','email')
widgets = {'password':forms.PasswordInput(),}
class Form4(forms.ModelForm):
class Meta:
model = user
fields =('user_code','phone','task')
感谢所有评论和答案。
尝试在每个表单中添加不同的前缀
#GET
user_form = userForm(prefix =one )
form = Form4(prefix =two)
#POST
user_form = userForm(request.POST,prefix =one)
form = Form4 (request.POST,prefix =two)
I am using the Django authentication system and have another model which is about user:
class user(models.Model):
user = models.OneToOneField(User)
user_code=models.IntegerField(max_length=3)
phone=models.IntegerField(max_length=10)
task=models.ForeignKey(task)
class Meta:
unique_together = ('user_code', 'task')
def __unicode__(self):
return str(self.user_code)
so I made two forms for user registeration(form.py):
class userForm(forms.ModelForm):
class Meta:
model=User
widgets = {'password': forms.PasswordInput(),}
class Form4(forms.ModelForm):
class Meta:
model=user
Now in my view I want to send two forms to template and submit both of them. Everything is well but after submitting I can't check form validation? this is my view:
def add_new_user(request):
if request.method == 'POST': # If the form has been submitted...
user_form = userForm(request.POST) # django User
form = Form4(request.POST) # my model user
if user_form.is_valid() and form.is_valid(): # All validation rules pass **edited** is_valide
user_name=user_form.cleaned_data['username']
password = user_form.cleaned_data['password']
name=user_form.cleaned_data['first_name']
user_id = form.cleaned_data['user_code']
email = user_form.cleaned_data['email']
phone = form.cleaned_data['phone']
task = form.cleaned_data['task']
else: # **added** form invalid statement
return render(request, 'student/form4.html', {'form': form,'user_form':user_form})
try: #**moved from valid statement**
do somthing...
except ValueError :
do somthing...
else:
form = Form4()
user_form=userForm()
# An unbound form
return render(request, 'student/form4.html', {'form': form,'user_form':user_form})
this is my template(form4.html):
<form name="myForm" action="{% url 'student:add_new_user' %}" onsubmit="return validateForm4();" method="post">
{% csrf_token %}
<div id="form-data">
{{ form.non_field_errors }}
{{ user_form.username }}{{ user_form.username.errors }}
<label for="id_user_name">user name:</label><br>
{{ user_form.password }}{{ user_form.password.errors }}
<label for="id_password">password:</label><br>
{{ user_form.first_name }}{{ user_form.first_name.errors }}
<label for="id_name">first name:</label><br>
{{ user_form.email }}{{ user_form.email.errors }}
<label for="id_email">email:</label><br>
{{ form.user_code }}{{ form.user_code.errors }}
<label for="id_email">use ID:</label><br>
{{ form.phone }}{{ form.phone.errors }}
<label for="id_phone">phone number:</label><br>
{{ form.task }}{{ form.task.errors }}
<label for="id_task">task:</label><br>
<input type="submit" value="register" /><br>
</div>
</form>
But it returns False for both validation. How can I check validation?
ANSWER:
I add which field I want from User and user. and change my "form.py" to :
class userForm(forms.ModelForm):
class Meta:
model=User
fields = ('username', 'password','first_name','email' )
widgets = {'password': forms.PasswordInput(),}
class Form4(forms.ModelForm):
class Meta:
model=user
fields = ('user_code', 'phone','task' )
thanks for all comments and answer.
Try add different prefixes to each of forms
# GET
user_form = userForm(prefix="one")
form = Form4(prefix="two")
# POST
user_form = userForm(request.POST, prefix="one")
form = Form4(request.POST, prefix="two")
这篇关于如何检查两种形式的验证在django形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!