[英]Django form is blank after submit
I have a simple form in django's template: 我在Django的模板中有一个简单的表单:
<div class="container">
<form action="{% url 'reporter:new_report' %}" method="post" novalidate>{% csrf_token %}
{{ report_form.as_p }}
<button type="submit" value="submit" class="btn btn-primary">Generuj raport</button>
</form>
</div>
but when I want submit it it send blank fields to my form and I don't know why. 但是,当我要提交它时,它将空白字段发送到我的表单,但我不知道为什么。
This is my views.py: 这是我的views.py:
def report_create(request, template_name='reporter/created.html'):
report = Report()
if request.POST:
report_form = ReportForm(request.POST or None, prefix="report_form")
if report_form.is_valid():
report = report_form.save(commit=False)
report.report_name = report_form.cleaned_data['report_name']
report.save()
return render(request, template_name, {})
return redirect('reporter:empty_form')
forms.py: forms.py:
class ReportForm(forms.ModelForm):
class Meta:
model = Report
fields = ['report_name', 'create_date', 'additional_findings',
'additional_recommendations', 'report_type']
and models.py: 和models.py:
class Report(models.Model):
report_name = models.CharField(max_length=100, blank=True)
create_date = models.DateField('date of creation', blank=True)
additional_findings = models.TextField(max_length=10000, blank=True)
additional_recommendations = models.TextField(max_length=10000, blank=True)
FULL_REPORT = 'FULL REPORT'
NOT_FULL_REPORT = 'NOT FULL REPORT'
REPORT_TYPE = (
(FULL_REPORT, 'Full report'),
(NOT_FULL_REPORT, 'Not full report')
)
report_type = models.CharField(max_length=100, choices=REPORT_TYPE, default=FULL_REPORT, blank=True)
I learn Django, I do everything like in tutorials, but I cannot understand why this form is still blank when I want submit it. 我学习Django,我会像教程中一样做所有事情,但是我不明白为什么要提交此表单时仍然空白。
EDIT: I changed report_create function but result i still same - data from a form is blank there. 编辑:我更改了report_create函数,但结果我仍然相同-表单中的数据在那里是空白的。
如果所有字段均有效,则可以使用report_form.cleaned_data
访问值
I finally solved problem. 我终于解决了问题。 I added
name="{{ field.name }}"
to the input fields in html file. 我在HTML文件的输入字段中添加了
name="{{ field.name }}"
。 And then print all values with cleaned_data
. 然后使用
cleaned_data
打印所有值。 It works fine. 工作正常。
EDIT: I changed {{ report_form.as_p }}
to the loop for over fields in form and added name
tag to inputs. 编辑:我将
{{ report_form.as_p }}
更改为表单中over字段的循环,并在输入中添加了name
标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.