Use Django Admin Widgets on any given page

According to the Django documentation Form Assets (the Media class) | Django documentation | Django

The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define asset requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those files that are required to render the widgets on any given page .

If you like the widgets that the Django Admin application uses, feel free to use them in your own application! They’re all stored in django.contrib.admin.widgets.

Here is my forms.py:

from django import forms
from django.contrib.admin.widgets import AdminDateWidget, AdminTimeWidget
from .models import Category, Event


class AntragForm(forms.Form):
    user = forms.CharField()
    day = forms.DateField(widget=AdminDateWidget(), label="Datum", required=True)
    category = forms.ModelChoiceField(
        queryset=Category.objects.all()
    )
    start_time = forms.TimeField(widget=AdminTimeWidget(), label="Start", required=False)
    end_time = forms.TimeField(widget=AdminTimeWidget(), label="Ende", required=False)
    whole_day = forms.BooleanField(label="Ganztag", required=False)
    private = forms.BooleanField(label="Privat", required=False)
    whole_week = forms.BooleanField(label="Ganze Woche", required=False)
    summary = forms.CharField(label="Grund", required=False)
    description = forms.CharField(widget=forms.Textarea, label="Beschreibung", required=False)

and here’s the part of the template:

<div class="row">
  <div class="col">
    {{ form.day|as_crispy_field}}
  </div>
</div>

I also tried without crispy-fields, but it doesn’t render either, so that’s not the problem. It simply doesn’t render the widgets. There is not error message either in the frontend. And yes, the widgets work correctly in Django Admin.

Note that the reference to a widget in a form field can be to the class, not an instance of the class. See the examples in the Widgets docs. (While you can pass an instance of the widget to the constructor, I’m going to guess that the information in the Media class isn’t going to be propagated through to the form in that situation. If you need to pass an instance, you might need to replicate the widget’s media class in your form to ensure the JavaScript assets get loaded.)