带有Django Crispy Forms的DateTimePicker [英] DateTimePicker with Django Crispy Forms
问题描述
我正在使用Python3,Django 1.5和Bootstrap 3.
I am using Python3, Django 1.5 and Bootstrap 3.
我有一个带有DateTimeField对象的模型,对于与该模型相关的表单,我想从此处使用DateTimePicker小部件:
I have a model with a DateTimeField object, and for the form related to that model, I want to use a DateTimePicker widget from here: http://www.malot.fr/bootstrap-datetimepicker/
此小部件的html应该为:
The html for this widget should be:
<div id="div_id_start_time" class="form-group">
<label for="id_start_time" class="control-label col-lg-2">Start Time</label>
<div class="col-lg-5 input-append input-group date form_datetime"
data-date-format="dd MM yyyy - HH:ii P" data-link-field="id_start_time">
<input class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
<input type="hidden" id="id_start_time" value="" /><br/>
</div>
</fieldset>
我正在尝试使用酥脆的形式,但是在获取特定的输出时遇到了麻烦.我觉得我需要为此小部件创建自己的布局对象,但是我不确定该怎么做,文档对此主题有些薄.
I'm trying to use crispy forms, but I'm having trouble getting it to make this particular output. I have a feeling I need to create my own layout object for this widget, but I'm not sure how to go about doing that, the docs are a little thin on this subject.
有什么想法吗?
推荐答案
我最终要做的就是为此小部件创建一个模板.所以代码看起来像这样:
What I ended up doing was to just create a template for this widget. So the code looks like this:
datetimefield.html
datetimefield.html
{% load crispy_forms_field %}
<div{% if div.css_id %} id="{{ div.css_id }}"{% endif %} class="form-group{% if form_show_errors and field.errors %} has-error{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}{% if div.css_class %} {{ div.css_class }}{% endif %}">
{% if field.label and form_show_labels %}
<label for="{{ field.id_for_label }}" class="control-label {{ label_class }}{% if field.field.required %} requiredField{% endif %}">
{{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% endif %}
<div class="controls input-group input-append date form-datetime col-md-5" data-link-field="{{ field.id_for_label }}" {{ flat_attrs|safe }}>
<input class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
<input type="hidden" id="{{ field.id_for_label }}" value="" /><br/>
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</div>
forms.py
self.helper.layout = Layout(
Field('start_time', template="layout/datetimefield.html", data_date_format="dd MM yyyy - HH:ii P"),
)
这篇关于带有Django Crispy Forms的DateTimePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!