我正在使用Django表单提交报告。但是单击提交按钮后,它将发送POST请求,但在POST请求中未添加任何数据。
这是我的表格。py
class WeeklyForm(forms.Form):
tname = forms.ModelMultipleChoiceField(queryset=CustomTeacher.objects.values_list('tname', flat = True), widget =forms.Select( attrs ={'class': 'form-control' , 'placeholder' : ' Teachers Name '}))
sname = forms.ModelMultipleChoiceField(queryset=CustomStudent.objects.values_list('sname', flat = True), widget =forms.CheckboxSelectMultiple( attrs ={'class': 'form-control' 'form-check-input' 'form-check-inline', 'placeholder' : ' Students Name '}))
class_name = forms.CharField(widget= forms.Select(choices= [('1', 'UKG'), ('2', 'Class 1'), ('3', 'LKG'), ('4', 'Montessori') ] ,attrs={'class': 'form-control', 'placeholder' : 'Select Class'}))
date = forms.DateField(initial = datetime.date.today() , required=False, widget =forms.DateInput( attrs ={'class': 'form-control' , 'placeholder' : ' Date ', 'name' : 'date'}))
objective = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'objective'}))
target = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'target'}))
how = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'how?'}))
material = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'material required'}))
support = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'Any Support Required?'}))
这是来自views.py的函数,我正在使用该函数获取数据并将其插入数据库
view.py
def weekly(request):
context = ''
form = WeeklyForm()
context = {'form': form}
if request.method == 'POST':
form = WeeklyForm(request.POST)
if form.is_valid():
tname1 = form.cleaned_data['teachers']
sname1 = form.cleaned_data['students']
date1 = form.cleaned_data['date']
objective1 = form.cleaned_data['objective']
tplan1 = form.cleaned_data['target']
how1 = form.cleaned_data['how']
material1 = form.cleaned_data['material']
extra1 = form.cleaned_data['support']
print (tname1)
report = wreport(tname = tname1, sname = sname1, fdate = date1, objective = objective1, tplan = tplan1, how = how1, material = material1, extra = extra1)
report.save()
messages.success(request, "Your report was submitted Successfully.")
else:
form = WeeklyForm()
context = {'form': form}
return render(request, 'teacher/weekly.html', context)
由于代码行很多,因此我仅显示模板的示例。 weekly.html
<form action="" method="POST">
{% csrf_token %}
<!-- Teacher section -->
<div class="row">
<div class="col-lg-3 mb-2">
<div><strong>Teacher's Name:</strong></div>
</div>
<div class="col-lg-9">
{{ form.teachers }}
</div>
</div>
<!-- Student section -->
<div class="row mt-4">
<div class="col-lg-3">
<!-- name="sname" id="sname" -->
<div><strong>Student's Name:</strong></div>
</div>
<div class="col-lg-9">
<div class="form-check-inline">
<label class="form-check-label">
<!-- {% for student in students %}
<input type="checkbox" class="form-check-input" value="">
{{student.sname}}
{% endfor %} -->
</label>
</div>
{{ form.sname }}
</div>
</div>
<!-- submit button -->
<div class="row">
<div class="mx-auto">
<button type="submit" class="btn btn-outline-dark mb-4 shadow" style="border-radius:25px;">Submit</button>
</div>
</div>
</form>
我已经尝试调试和打印我从表单接收到的值,但是什么也没打印出来。
编辑:进一步调试后,我发现我的表单无法验证这是我使用form.errors
函数捕获的错误
<ul class="errorlist"><li>tname<ul class="errorlist"><li>Enter a list of values.</li></ul></li><li>sname<ul class="errorlist"><li>“Test Student” is not a valid value.</li></ul></li></ul>
答案 0 :(得分:0)
第一个:当您使用queryset属性时,您需要分配查询集,而每当使用choices属性时,您都需要分配列表或字典
第二:,当您尝试请求相同的URL时。您必须删除表单中的操作
,并且您想在另一个URL上发出请求。您必须添加正在调用的视图的操作网址
这是form.py
class WeeklyForm(forms.Form):
tname = forms.ModelMultipleChoiceField(queryset=CustomTeacher.objects.all(), widget =forms.Select( attrs ={'class': 'form-control' , 'placeholder' : ' Teachers Name '}))
sname = forms.ModelMultipleChoiceField(queryset=CustomStudent.objects.all(), widget =forms.CheckboxSelectMultiple( attrs ={'class': 'form-control' 'form-check-input' 'form-check-inline', 'placeholder' : ' Students Name '}))
class_name = forms.CharField(widget= forms.Select(choices= [('1', 'UKG'), ('2', 'Class 1'), ('3', 'LKG'), ('4', 'Montessori') ] ,attrs={'class': 'form-control', 'placeholder' : 'Select Class'}))
date = forms.DateField(initial = datetime.date.today() , required=False, widget =forms.DateInput( attrs ={'class': 'form-control' , 'placeholder' : ' Date ', 'name' : 'date'}))
objective = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'objective'}))
target = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'target'}))
how = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'how?'}))
material = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'material required'}))
support = forms.CharField(widget = forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'Any Support Required?'}))
在这里,我认为是同一网址上的要求
<form method="POST">
{% csrf_token %}
<!-- Teacher section -->
<div class="row">
<div class="col-lg-3 mb-2">
<div><strong>Teachers Name:</strong></div>
</div>
<div class="col-lg-9">
{{ form.teachers }}
</div>
</div>
<!-- submit button -->
<div class="row">
<div class="mx-auto">
<button type="submit" class="btn btn-outline-dark mb-4 shadow" style="border-radius:25px;">Submit</button>
</div>
</div>
</form>
这里是视图
def weekly(request):
context = ''
form = WeeklyForm()
context = {'form': form}
if request.method == 'POST':
form = WeeklyForm(request.POST)
if form.is_valid():
data = form.cleaned_data
print (data)
report = wreport(tname = data['teachers'], sname = data['students'], fdate = data['date'], objective = data['objective'], tplan = data['target'], how = data['how'], material = data['material'], extra = data['support'])
report.save()
messages.success(request, "Your report was submitted Successfully.")
else:
form = WeeklyForm()
context = {'form': form}
return render(request, 'teacher/weekly.html', context)
希望您能理解我想说的话,让我知道任何查询问题