Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using date picker from django admin

I'm new to Django and I'm trying to find the Django Admin date picker widget. I have search but got no results. Could anyone help me to use it on the project I'm working on?

I have a datetimefield in my model, like this:

fin = models.DateTimeField()

When creating a model from the admin is shows the widget but when I do it from the project it doesn't.

like image 691
Rodrigo Munoz Avatar asked Mar 03 '26 23:03

Rodrigo Munoz


2 Answers

Specify the widget in your form as follows:

from django.contrib.admin.widgets import AdminDateWidget

class YourForm(forms.ModelForm):
        from_date = forms.DateField(widget=AdminDateWidget())

Don't forget to include the scripts and css in your template:

{{ form.media }}
like image 178
Alex Morozov Avatar answered Mar 06 '26 15:03

Alex Morozov


If for some reason you are looking to use the date picker with a PostgreSQL DateRangeField, you can do so like so:

from django.contrib.admin.widgets import AdminDateWidget
from django.contrib.postgres.forms.ranges import DateRangeField, 
RangeWidget

class YourForm(forms.ModelForm):
    date_range = DateRangeField(widget=RangeWidget(AdminDateWidget()))
like image 24
Ben Avatar answered Mar 06 '26 15:03

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!