我想使用Django表单从预构建的HTML模板中获取表单数据,而无需更改显示。
我知道我可以使用类似的东西:
name = request.POST.get('first_name')
但是想使用ModelForm处理数据
class BioDataForm(forms.ModelForm):
first_name = forms.CharField(max_length=100, label='first_name')
last_name =forms.CharField(max_length=100, label='last_name')
mobile = forms.CharField(max_length=100, label='mobile_number')
email = forms.EmailField(max_length=100, label='personal_email')
gender = forms.CharField(max_length=100, widget=forms.Select(attrs={'class': 'form-control', 'id': 'gender'}))
marital_status = forms.CharField(max_length=100, widget=forms.Select(attrs={'class': 'form-control', 'id': 'marital_status'}))
date_of_birth = forms.DateField(input_formats=['%Y-%m-%d'], widget=forms.SelectDateWidget())
address = forms.CharField(max_length=100)
class Meta:
model = BioData
exclude = ('state',)
fields = ['first_name', 'last_name', 'mobile', 'email',
'gender', 'marital_status', 'date_of_birth', 'address']
<div class="row form-group">
<div class="col-md-6 col-sm-12 margin-bottom-20-m">
<label for="first_name" class="form-control-label kt-black-text">First Name</label>
<input type="text" class="form-control" id="first_name" name="e_first_name" required>
</div>
<div class="col-md-6 col-sm-12">
<label for="last_name" class="form-control-label kt-black-text">Last Name</label>
<input type="text" class="form-control" id="last_name" name="e_last_name" required>
</div>
</div>
答案 0 :(得分:0)
模板中的迭代形式:
<div class="row form-group">
{% for field in form %}
<div class="col-md-6 col-sm-12 margin-bottom-20-m">
<label for="{{ field.id_for_label }}" class="form-control-label kt-black-text">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
</div>