Python Django表单不关心html输入
在django项目中,我创建了一个用于管理用户注册的表单,下面是我的代码: models.py:Python Django表单不关心html输入,python,django,python-3.x,django-forms,Python,Django,Python 3.x,Django Forms,在django项目中,我创建了一个用于管理用户注册的表单,下面是我的代码: models.py: class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE,) u_fullname = models.CharField(max_length=200) u_email = models.EmailField() u_profile = mod
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,)
u_fullname = models.CharField(max_length=200)
u_email = models.EmailField()
u_profile = models.CharField(max_length=1)
u_type = models.CharField(max_length=1, default='F')
u_job = models.CharField(max_length=100, null=True, blank=True, default='D')
u_country = models.CharField(max_length=20, null=True, blank=True, default='Italy')
u_regdata = models.DateTimeField(auto_now=True)
u_picture = models.ImageField(upload_to='profile_images', blank=True)
u_active = models.BooleanField(default=False)
u_terms = models.BooleanField(default=False)
def __unicode__(self):
return self.u_profile
forms.py
class ProfileModelForm(ModelForm):
class Meta:
model = UserProfile
fields = ['u_fullname',
'u_job',
'u_country',
'u_email',
'u_terms',
]
def clean(self):
cleaned_data = super(ProfileModelForm, self).clean()
u_fullname = cleaned_data.get('u_fullname')
u_job = cleaned_data.get('u_job')
u_country = cleaned_data.get('u_country')
u_email = cleaned_data.get('u_email')
u_terms = cleaned_data.get('u_terms')
if not u_terms:
raise forms.ValidationError("Please read and accept our Terms of Service")
return u_terms
if not u_fullname and not u_job and not u_country and not u_terms:
raise forms.ValidationError('You have to write something!')
视图.py
if request.method == 'POST':
if 'user_reg' in request.POST:
form = ProfileModelForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
#Create user and get the id
n_user = User.objects.create_user(username=request.POST['u_email'],
email=request.POST['u_email'],
password=request.POST['u_password'])
instance.user = User.objects.get(id=n_user.id)
instance.u_profile = 'U'
instance.save()
return
else:
messages.error(request, "Error")
在“我的html表单”部分的末尾:
<form action="" method="POST">
{% csrf_token %}
{{ form.errors }}
<div class="row">
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="text" name="u_fullname" placeholder="Full Name" value={{ form.u_fullname }}>
<i class="la la-user"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<select value={{ form.u_country }}>
<option selected="selected">Italy</option>
<option>Spain</option>
<option>USA</option>
<option>France</option>
</select>
<i class="la la-globe"></i>
<span><i class="fa fa-ellipsis-h"></i></span>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<select value={{ form.u_job }}>
<option value="D" selected="selected">Developer</option>
<option value="T">Tester</option>
<option value="S">System Administrator</option>
<option value="V">Sales</option>
</select>
<i class="la la-dropbox"></i>
<span><i class="fa fa-ellipsis-h"></i></span>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="text" name="u_email" placeholder="Enter a valid email" value="{{ form.u_email }}">
<i class="la la-envelope"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="password" name="u_password" placeholder="Password">
<i class="la la-lock"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="password" name="repeat-password" placeholder="Repeat Password">
<i class="la la-lock"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="checky-sec st2">
<div class="fgt-sec">
<input type="checkbox" name="cc" id="c2" value={{ form.u_terms }}>
<label for="c2">
<span></span>
</label>
<small>Yes, I understand and agree to the workwise Terms & Conditions.</small>
</div><!--fgt-sec end-->
</div>
</div>
<div class="col-lg-12 no-pdd">
<button type="submit" name="user_reg" value="submit">Get Started</button>
</div>
</div>
</form>
{%csrf_令牌%}
{{form.errors}}
意大利
西班牙
美国
法国
开发商
测试员
系统管理员
销售额
是的,我理解并同意workwise条款和条件。
开始
好吧,现在我的问题是,对于输入的和字段数据,似乎没有考虑在内;每次选择,或者如果选中数据库中的复选框,我都会找到模型默认值。
如何解决正确管理表单中所有字段的问题
提前非常感谢问题在于
clean
方法中的重写;此方法应返回与所有字段相关的数据:
class ProfileModelForm(ModelForm):
# ...
def clean(self):
cleaned_data = super(ProfileModelForm, self).clean()
u_fullname = cleaned_data.get('u_fullname')
u_job = cleaned_data.get('u_job')
u_country = cleaned_data.get('u_country')
u_email = cleaned_data.get('u_email')
u_terms = cleaned_data.get('u_terms')
if not u_terms:
raise forms.ValidationError("Please read and accept our Terms of Service")
return cleaned_data
# This would never run:
if not u_fullname and not u_job and not u_country and not u_terms:
raise forms.ValidationError('You have to write something!')
class ProfileModelForm(ModelForm):
# ...
def清洁(自清洁):
cleaned_data=super(ProfileModelForm,self).clean()
u\u fullname=cleaned\u data.get('u\u fullname'))
u\u job=已清理的数据。获取('u\u job')
u\u country=已清理的数据。获取('u\u country')
u_email=已清理的数据。获取('u_email')
u\u terms=已清理的数据。获取('u\u terms')
如果不是u_条款:
提出表格。验证错误(“请阅读并接受我们的服务条款”)
返回已清除的数据
#这永远不会运行:
如果不是u_全名,也不是u_职务,也不是u_国家,也不是u_术语:
raise forms.ValidationError('你必须写点什么!')
还要记住,您的第二个
if
语句永远不会被检查,因为它在return
语句之后。问题在于clean
方法中的重写;此方法应返回与所有字段相关的数据:
class ProfileModelForm(ModelForm):
# ...
def clean(self):
cleaned_data = super(ProfileModelForm, self).clean()
u_fullname = cleaned_data.get('u_fullname')
u_job = cleaned_data.get('u_job')
u_country = cleaned_data.get('u_country')
u_email = cleaned_data.get('u_email')
u_terms = cleaned_data.get('u_terms')
if not u_terms:
raise forms.ValidationError("Please read and accept our Terms of Service")
return cleaned_data
# This would never run:
if not u_fullname and not u_job and not u_country and not u_terms:
raise forms.ValidationError('You have to write something!')
class ProfileModelForm(ModelForm):
# ...
def清洁(自清洁):
cleaned_data=super(ProfileModelForm,self).clean()
u\u fullname=cleaned\u data.get('u\u fullname'))
u\u job=已清理的数据。获取('u\u job')
u\u country=已清理的数据。获取('u\u country')
u_email=已清理的数据。获取('u_email')
u\u terms=已清理的数据。获取('u\u terms')
如果不是u_条款:
提出表格。验证错误(“请阅读并接受我们的服务条款”)
返回已清除的数据
#这永远不会运行:
如果不是u_全名,也不是u_职务,也不是u_国家,也不是u_术语:
raise forms.ValidationError('你必须写点什么!')
还要记住,您的第二个if
语句永远不会被检查,