Django:提交表单时可能不是NULL

时间:2015-07-17 14:36:13

标签: python django forms

我尝试提交一个仅包含日期(现在是时间)的表单,但是当我提交它时,即使我在输入字段中输入实际日期,我也会收到错误incidents_incident.incident_date_time_occurred may not be NULL 。输入日期时为什么会出现错误?

models.py

class Incident(models.Model):
       incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now, blank=True)
    class Meta:
               verbose_name_plural="Incident"

forms.py

class IncidentForm(forms.ModelForm):

    class Meta:
        model = Incident
        fields = ('incident_date_time_occurred',)

report.html

{% extends "base.html" %}

{% block content %}

<div class="container-fluid">

<form action="{% url 'incidents:report' %}" method="POST">{% csrf_token %}
    {% csrf_token %}

    <div class="row">
        <div class="form-group col-md-4">
            <label for="date" class="col-sm-4 control-label">{{ form.incident_date_time_occurred.label }}</label>
            <input type="datetime-local" name= "incident_date_time_occurred" class="form-control" id="date">
        </div>
    </div>

    <input type="submit" value="Inschrijven!" class="btn btn-primary" />

</form>

</div>
{% endblock %}

2 个答案:

答案 0 :(得分:0)

您可以使用从事件创建的默认表单来检查您的解决方案吗?我记得Django的dateTimeField需要两个字段:日期和时间。

https://docs.djangoproject.com/en/1.7/ref/models/fields/#datetimefield

答案 1 :(得分:0)

您在日期输入中缺少name属性。试试这个:

<input type="date" class="form-control" id="date" name="incident_date_time_occurred">