0

I am using django.contrib.postgres.field.DateRangeField as described here.

class MyModel(models.Model):
    date_range = DateRangeField()

I am rendering this in a template:

{% with field = form.date_range %}
{{ field.label }}
{{ field }}
{% endwith %}

However, this outputs 2 inputs together with no context. E.g.:

enter image description here

How can I edit the default template for the widget, to add "sub" labels for "start time" and "end time". E.g: enter image description here

There does not appear to be anything I can find in the docs.

alias51
  • 8,178
  • 22
  • 94
  • 166
  • 2
    Have you tried [custom django widget](https://stackoverflow.com/questions/4707192/django-how-to-build-a-custom-form-widget)? – JPG Oct 03 '21 at 16:22
  • 1
    https://docs.djangoproject.com/en/3.2/ref/forms/renderers/#overriding-built-in-widget-templates – lucutzu33 Oct 03 '21 at 16:24
  • @JPG Yes but DateRangeField doesn't take a template input, so I don't know where to start. See here: https://github.com/django/django/blob/c2f6c05c4cc73e831b7e852eb58bd6d7a83fa46c/django/contrib/postgres/forms/ranges.py#L99 – alias51 Oct 03 '21 at 16:27
  • @EneP Thanks, but same problem as JPG, the ModeField doesn't take an input – alias51 Oct 03 '21 at 16:27
  • 1
    model field don't take the input, but, the form field does - [see this](https://stackoverflow.com/questions/9878475/django-modelform-override-widget) – JPG Oct 03 '21 at 16:29
  • It takes `widget` as an argument. Then you have to create your custom widget with a custom template. – lucutzu33 Oct 03 '21 at 16:29
  • @JPG so how does one separate the two inputs in the widget template? In the django source code it just repeats the same `base_field` – alias51 Oct 03 '21 at 16:47

0 Answers0