我的django表格无效。我想这是因为request.POST几乎是空的(只包括csrf_token)。我仍然无法找到原因。寻求帮助。
P.S。 (为了说清楚,代码中发生了什么)我有三个级别的级联类别和一个模式列表。我想用一个类别标记所有模式。 select2的选项取决于select1中的选定选项。对于select3也是如此。
表格:
class AddPatternForm(forms.Form):
category_1 = forms.ChoiceField()
category_2 = forms.ChoiceField(required=False)
category_3 = forms.ChoiceField(required=False)
patterns = forms.CharField()
def verify(self):
valid = []
invalid = []
patterns = self.patterns.split('\n')
for pattern in patterns:
if VALID_DOMAIN.match(pattern):
valid.append(get_domain(pattern))
elif VALID_REGEXP.match(pattern):
valid.append(pattern)
else:
invalid.append(pattern)
return valid, invalid
观点:
def add_pattern(request):
patterns = Url.objects.all()
parents = Category.objects.filter(parent=None)
if request.method == 'POST':
form = AddPatternForm(request.POST)
if form.is_valid():
valid, invalid = AddPatternForm.verify()
if form.category_3:
parent_id = form.category_3
elif form.category_2:
parent_id = form.category_2
else:
parent_id = form.category_1
parent = Category.objects.get(pk=parent_id)
for pattern in valid:
Url(pattern=pattern, categories=parent).save()
return HttpResponseRedirect('')
else:
form = AddPatternForm()
return render(request, 'app/add_pattern.html', {
'form': form,
'patterns': patterns,
'parents': parents,
})
html:
<form id="chooseCategory" method="post" action="">
{% csrf_token %}
<div class="form-group">
<h2 id="chosenCategory">Категория: </h2>
<select id="select1" class="form-control">
<option value="">---</option>
{% for parent in parents %}
<option value="{{ parent.id }}">
{{ parent }}
</option>
{% endfor %}
</select>
<option value="">---</option>
<select id="select2" class="form-control">
</select>
<option value="">---</option>
<select id="select3" class="form-control">
</select>
</div>
<div class="form-group">
<textarea class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
JS(实现级联下拉选择):
$( "#select1" ).change(function () {
var str = "Категория: ";
$( "#select1 option:selected" ).each(function() {
str += $( this ).text() + " ";
});
$( "#chosenCategory" ).text( str );
$( "#select2" ).find('option').remove().end()
$( "#select2" ).append("<option value=''>---</option>")
$.get("/load_new_options/", {"parent_id": $( "#select1 option:selected" ).val()},
function(data) {
var c = JSON.parse(data);
for (i in c) {
$( "#select2" ).append("<option value='" + c[i].pk + "'>" + c[i].fields.name + "</option>")
}
})
})
.change();
$( "#select2" ).change(function () {
var str = "Категория: ";
$( "#select2 option:selected" ).each(function() {
str += $( this ).text() + " ";
});
$( "#chosenCategory" ).text( str );
$( "#select3" ).find('option').remove().end()
$( "#select3" ).append("<option value=''>---</option>")
$.get("/load_new_options/", {"parent_id": $( "#select2 option:selected" ).val()},
function(data) {
var c = JSON.parse(data);
for (i in c) {
$( "#select3" ).append("<option value='" + c[i].pk + "'>" + c[i].fields.name + "</option>")
}
})
})
.change();
$( "#select3" ).change(function () {
var str = "Категория: ";
$( "#select3 option:selected" ).each(function() {
str += $( this ).text() + " ";
});
$( "#chosenCategory" ).text( str );
})
.change();
$('#chooseCategory').cascadingDropdown({
selectBoxes: [
{
selector: '#select1',
},
{
selector: '#select2',
requires: ['#select1'],
},
{
selector: '#select3',
requires: ['#select1', '#select2'],
requireAll: true,
onChange: function(){
}
}
]
});
UPD 表格仍无效。 form.errors如下所示:
category_3
Select a valid choice. 19 is not one of the available choices.
category_2
Select a valid choice. 4 is not one of the available choices.
category_1
Select a valid choice. 1 is not one of the available choices.
答案 0 :(得分:1)
{{ form.errors }}
修改您尚未在表单字段元素中添加任何name
属性。如果字段具有名称,浏览器仅在表单提交上发送数据,因为数据与数据相关联。
你真的应该使用Django表单来输出字段,而不是手动创建HTML。