Skip to content

Commit 15d65a8

Browse files
authored
Merge pull request #27 from carllopez/master
Handling properly datetime fields
2 parents 7c6bdca + a028da6 commit 15d65a8

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ This is most useful for adding a descriptive icon when you are creating a custom
103103
{{ form.FIELD_NAME|materializecss:'custom_size=s12 m6, icon=person' }}
104104
```
105105

106+
### Note about `DateTimeField`
107+
Input field is rendered as a *datetime-local* type, that let the user easily enter both a date and a time. As this field requires ISO-8601 format, your main project settings need to include the ISO format in order for the form to consider this field valid:
108+
```
109+
from django.conf.global_settings import DATETIME_INPUT_FORMATS
110+
111+
# ISO 8601 datetime format to accept html5 datetime input values
112+
DATETIME_INPUT_FORMATS += ["%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M"]
113+
```
114+
106115
## Demo
107116

108117
![Basic form](https://cloud.githubusercontent.com/assets/3958123/6165004/a1984f52-b2a4-11e4-8ae2-078505991b0d.png)

materializecssform/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from distutils.version import StrictVersion
22

33

4-
VERSION = StrictVersion('1.1.10')
4+
VERSION = StrictVersion('1.1.11')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% for name, value in field.field.widget.attrs.items %}
2-
{% if value is not False %}
2+
{% if value is not False %}
33
{{ name }}{% if value is not True %}="{{ value|stringformat:'s' }}" {% endif %}
44
{% endif %}
55
{% endfor %}

materializecssform/templates/materializecssform/field.html

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
{% endif %}
4848
</div>
4949
</div>
50-
{% elif field|is_date_input or field|is_datetime_input %}
50+
{% elif field|is_date_input %}
5151
<div class="input-field col {{ classes.label }} {{ classes.value }} {{ classes.single_value }}">
5252
{% if classes.icon %}
5353
<i class="material-icons prefix">{{ classes.icon }}</i>
@@ -66,6 +66,26 @@
6666
</p>
6767
{% endif %}
6868

69+
</div>
70+
{% elif field|is_datetime_input %}
71+
<div class="input-field col {{ classes.label }} {{ classes.value }} {{ classes.single_value }}">
72+
{% if classes.icon %}
73+
<i class="material-icons prefix">{{ classes.icon }}</i>
74+
{% endif %}
75+
76+
<input type="datetime-local" id="{{ field.auto_id }}" name="{{ field.html_name }}" value="{% if field.value %}{{ field.value|date:'c' }}{% endif %}" {% include 'materializecssform/attrs.html' %} >
77+
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
78+
79+
{% for error in field.errors %}
80+
<span class="help-block {{ form.error_css_class }}">{{ error }}</span>
81+
{% endfor %}
82+
83+
{% if field.help_text %}
84+
<p class="help-block">
85+
{{ field.help_text|safe }}
86+
</p>
87+
{% endif %}
88+
6989
</div>
7090
{% elif field|is_select %}
7191
<div class="input-field col {{ classes.label }} {{ classes.value }} {{ classes.single_value }}">

0 commit comments

Comments
 (0)