Python Django表单在提交后为空
我在django的模板中有一个简单的表单:Python Django表单在提交后为空,python,django,forms,submit,Python,Django,Forms,Submit,我在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-prima
<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>
forms.py:
class ReportForm(forms.ModelForm):
class Meta:
model = Report
fields = ['report_name', 'create_date', 'additional_findings',
'additional_recommendations', 'report_type']
和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)
我学习Django,我做的一切都像在教程中一样,但我不明白为什么当我想要提交它时,这个表单仍然是空白的
编辑:
我更改了report\u create函数,但结果仍然相同-表单中的数据为空。如果您的所有字段都有效,那么您可以使用
报表访问值。清理的报表数据
如果您的所有字段都有效,那么您可以使用报表访问值。清理的报表数据
我最终解决了问题。
我在html文件的输入字段中添加了name=“{{field.name}}”
。然后用已清理的\u数据打印所有值
。它很好用
编辑:
我将{{report\u form.as\p}}
更改为表单中over字段的循环,并将name
标记添加到输入中。我最终解决了这个问题。
我在html文件的输入字段中添加了name=“{{field.name}}”
。然后用已清理的\u数据打印所有值
。它很好用
编辑:
我将
{{report\u form.as\p}}
更改为表单中over字段的循环,并将name
标记添加到输入中。我不明白。我得到一个错误:AttributeError:“ReportForm”对象没有“cleaned_data”属性。你能解释一下我该怎么办吗?天哪!对不起,我刚才没提到。。。只有在有效后,您才应该清理数据
,但我认为这不是一个解决方案。表单中的数据不会提交到视图。“已清理的数据”在这方面有何帮助?在“有效”之后,您是否可以打印报告表格。已清理的数据['report\u name']
这一点?我不明白。我得到一个错误:AttributeError:“ReportForm”对象没有“cleaned_data”属性。你能解释一下我该怎么办吗?天哪!对不起,我刚才没提到。。。只有在有效后,您才应该清理数据
,但我认为这不是一个解决方案。表单中的数据不会提交到视图。“清理过的数据”在这方面有什么帮助?在“清理过的数据”有效后,你可以打印报告表单。清理过的数据['report\u name']
这个?我以为你是用Django表单构建html输入的,但不是啊?一开始,是的。然后我将django{{report_form.as_p}}
更改为该表单中的for循环域,并在输入中添加了name
标记。我以为您使用django表单构建html输入,但不是ah?一开始,是的。然后我将django{{report_form.as_p}}
更改为该表单中的for循环域,并将name
标记添加到输入中。
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)