"输入有效时间"在Django TimeField上使用django-bootstrap3-datetime

时间:2014-04-11 03:29:24

标签: python django twitter-bootstrap django-models django-forms

使用django bootstrap日期时间选择器提交表单时,我遇到时间验证问题。提交此表单后,我会收到“输入有效时间”。错误

models.py:

class Appointment(models.Model):
    date = models.DateField()
    time = models.TimeField()

forms.py:

valid_time_formats = ['%P', '%H:%M%A', '%H:%M %A', '%H:%M%a', '%H:%M %a']

date = forms.DateField(
    widget=DateTimePicker(options={"format": "MM/DD/YYYY",
                                   "pickTime": False,
                                   "showToday": True,
                                   }))

time = forms.TimeField(
    widget=DateTimePicker(options={
                                   "pickTime": True,
                                   "pickDate": False,
                                   "minuteStepping": 1,
                                   "sideBySide": True,
                                   }),
    input_formats = valid_time_formats
    )


class Meta:
    model = Appointment
    fields = (
        'date',
        'time',
        'duration',
        'cost',
    )

如果有帮助,可以使用模板:

{% extends 'scheduling/base_scheduling.html' %}
{% load bootstrap3 %}

{% block title %}New Appointment{% endblock %}

{% block external %}

        <link rel="stylesheet"
              href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.css">
        <link rel="stylesheet"
              href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.js">
        </script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.js">
        </script>

        {{ form.media }}

{% endblock %}

{% block content %}


    {# Load CSS and JavaScript #}

    {% bootstrap_css %}
    {% bootstrap_javascript %}

    {# Display django.contrib.messages as Bootstrap alerts #}
    {% bootstrap_messages %}

    {# Display a form #}

    <h1>New Appointment for {{client.full_name}}</h1>
    <form action="{% url 'client_appointment_add' client.id %}" method="post" class="form">
        {% csrf_token %}
        {% bootstrap_form form %}
        {% buttons %}
            <button type="submit" class="btn btn-primary">
                {% bootstrap_icon "save" %} Submit
            </button>
        {% endbuttons %}
    </form>

{% endblock %}

1 个答案:

答案 0 :(得分:0)

我有同样的错误。在表单中设置日期格式是不够的。

在正确配置django datetime设置后,我摆脱了它。

如何正确查看我的答案:How to format DateTimeField in Django Admin to localtime? 你会发现还有一个链接到带有正确配置的示例项目。

我现在使用bootstrap3_datetimebootstrap3对此进行了测试,但它确实有用,所以它也适用于您。