How do I get a specific field e.g the date-field in SplitDateTimeField?

I have a form with a SplitDateTimeField:


class EventCreationForm(forms.ModelForm):
    start_hosting_date_and_time = forms.SplitDateTimeField(input_date_formats=["%d-%m-%Y"], input_time_formats=["%H:%M"], 
                                                           widget = forms.widgets.SplitDateTimeWidget( date_attrs = {"placeholder": "dd-mm-yyyy"}, 
                                                                                                      time_attrs= {"placeholder": "TT:MM"}))

and then in my template

            <!-- Hosting Date -->
            <div class="form-group">
                <label for="start_hosting_date_and_time"><i class="fas fa-calendar-alt"></i> Event dato</label>
                {% if form.start_hosting_date_and_time.errors %}
                <div class="error">
                    {% for error in form.start_hosting_date_and_time.errors %}
                        {{ error }}
                    {% endfor %}
                </div>
                {% endif %}
                <div class = "start_hosting_date_and_time">
                        {{ form.start_hosting_date_and_time}}
                </div>
            </div>

now, I want to control the date/time more finegrained i.e I want to create a div for each of them. But I can’t figure out how do “index” them (and I can’t find anything in the django documentation)

I’ve tried accessing the date like {{ form.start_hosting_date_and_time.0}} but that gives me the same widget as just {{ form.start_hosting_date_and_time}}. I have tried various other stuff like {{ form.start_hosting_date_and_time.date}} and such but nothing seems to work.

Any ideas (and if doable, a link to the documentation since it must be written somewhere)?

You could subclass SplitDateTimeWidget and supply your own template for the widget.

class MySplitDateTimeWidget(SplitDateTimeWidget):
    template_name = "mysplitdatetimewidget.html"

class EventCreationForm(forms.ModelForm):
    start_hosting_date_and_time = forms.SplitDateTimeField(
        input_date_formats=["%d-%m-%Y"],
        input_time_formats=["%H:%M"], 
        # Only changed the name of the widget:
        widget = MySplitDateTimeWidget(
            date_attrs={"placeholder": "dd-mm-yyyy"},
            time_attrs={"placeholder": "TT:MM"},
        )
    )

The default template is at templates/django/forms/widgets/splitdatetime.html and does nothing but include templates/django/forms/widgets/multiwidget.html, which looks like:

{% spaceless %}{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}{% endspaceless %}

You could copy that to your custom widget’s template and add <div> tags around the include in that.

Instead of creating a custom widget, you could put your custom template at templates/django/forms/widgets/splitdatetime.html which would override the template for all uses of SplitDateTimeWidget in your project.

There are docs about working with Form templates, and then rendering fields manually here: Working with forms | Django documentation | Django

The direct answer to your question is “No”. There’s no direct method to access the individual form fields - they’re treated as a single unit within the field. (They are defined as two separate widgets internally, but rendered together.) You can alter the widgets and the template(s) used to render it, but the widgets are going to be rendered together within the template.

If you need to handle these fields individually, then you will want to set up two separate fields within the form, split the model field in the __init__ method to populate them initially and use the clean method to combine them when submitted.