184

How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?

I have looked through the Django forms documentation, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use it?

Here is my template that I want it applied on.

<form action="." method="POST">
    <table>
        {% for f in form %}
           <tr> <td> {{ f.name }}</td> <td>{{ f }}</td> </tr>
        {% endfor %}
    </table>
    <input type="submit" name="submit" value="Add Product">
</form>

Also, I think it should be noted that I haven't really written a view up myself for this form, I am using a generic view. Here is the entry from the url.py:

(r'^admin/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}),

And I am relevantly new to the whole Django/MVC/MTV thing, so please go easy...

dfarrell07
  • 2,872
  • 2
  • 21
  • 26
Josh Hunt
  • 14,225
  • 26
  • 79
  • 98

17 Answers17

169

The growing complexity of this answer over time, and the many hacks required, probably ought to caution you against doing this at all. It's relying on undocumented internal implementation details of the admin, is likely to break again in future versions of Django, and is no easier to implement than just finding another JS calendar widget and using that.

That said, here's what you have to do if you're determined to make this work:

  1. Define your own ModelForm subclass for your model (best to put it in forms.py in your app), and tell it to use the AdminDateWidget / AdminTimeWidget / AdminSplitDateTime (replace 'mydate' etc with the proper field names from your model):

     from django import forms
     from my_app.models import Product
     from django.contrib.admin import widgets                                       
    
     class ProductForm(forms.ModelForm):
         class Meta:
             model = Product
         def __init__(self, *args, **kwargs):
             super(ProductForm, self).__init__(*args, **kwargs)
             self.fields['mydate'].widget = widgets.AdminDateWidget()
             self.fields['mytime'].widget = widgets.AdminTimeWidget()
             self.fields['mydatetime'].widget = widgets.AdminSplitDateTime()
    
  2. Change your URLconf to pass 'form_class': ProductForm instead of 'model': Product to the generic create_object view (that'll mean from my_app.forms import ProductForm instead of from my_app.models import Product, of course).

  3. In the head of your template, include {{ form.media }} to output the links to the Javascript files.

  4. And the hacky part: the admin date/time widgets presume that the i18n JS stuff has been loaded, and also require core.js, but don't provide either one automatically. So in your template above {{ form.media }} you'll need:

     <script type="text/javascript" src="/my_admin/jsi18n/"></script>
     <script type="text/javascript" src="/media/admin/js/core.js"></script>
    

You may also wish to use the following admin CSS (thanks Alex for mentioning this):

    <link rel="stylesheet" type="text/css" href="/media/admin/css/forms.css"/>
    <link rel="stylesheet" type="text/css" href="/media/admin/css/base.css"/>
    <link rel="stylesheet" type="text/css" href="/media/admin/css/global.css"/>
    <link rel="stylesheet" type="text/css" href="/media/admin/css/widgets.css"/>

This implies that Django's admin media (ADMIN_MEDIA_PREFIX) is at /media/admin/ - you can change that for your setup. Ideally you'd use a context processor to pass this values to your template instead of hardcoding it, but that's beyond the scope of this question.

This also requires that the URL /my_admin/jsi18n/ be manually wired up to the django.views.i18n.javascript_catalog view (or null_javascript_catalog if you aren't using I18N). You have to do this yourself instead of going through the admin application so it's accessible regardless of whether you're logged into the admin (thanks Jeremy for pointing this out). Sample code for your URLconf:

(r'^my_admin/jsi18n', 'django.views.i18n.javascript_catalog'),

Lastly, if you are using Django 1.2 or later, you need some additional code in your template to help the widgets find their media:

{% load adminmedia %} /* At the top of the template. */

/* In the head section of the template. */
<script type="text/javascript">
window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";
</script>

Thanks lupefiasco for this addition.

djvg
  • 11,722
  • 5
  • 72
  • 103
Carl Meyer
  • 122,012
  • 20
  • 106
  • 116
  • 3
    Most helpful post online for doing this but having finally got date/time widget appear and populating field am now going to take it out because despite having required=False in the form field it now shows the message "Enter a valid date/time." for my not required field if I leave it blank. Back to jquery for me. – PhoebeB Oct 01 '09 at 09:46
  • Is this the way to go on Django 1.1.1? – Ignacio Mar 16 '10 at 02:39
  • @Ignacio I haven't tested this for quite a while, so YMMV. I'm not aware of any easier way that's been introduced since, in any case. – Carl Meyer Mar 16 '10 at 03:49
  • 1
    In Django 1.2 RC1 and later, you need to make a small change to the above. See: http://stackoverflow.com/questions/38601/using-django-time-date-widgets-in-custom-form/2818128#2818128 – mshafrir May 12 '10 at 11:11
  • 1
    Do you know if this still applies to Django 1.4? Although I'm convinced to go with jQuery's datepicker, I'm curious if the Django team might have made their widget more publicly available. (I suspect not, as this QA seems to be the most documentation around) – John C Oct 25 '12 at 14:29
  • 1
    There has been no movement towards making the admin widgets more easily reusable outside the admin, and I wouldn't guess that there will be any. It would be a lot of work, and there are much higher priority items to work on. (I'm part of the Django core team, btw). – Carl Meyer Oct 25 '12 at 16:20
  • this is great answer and it worked for me in django version 1.4. but recently i needed to convert django 1.4 to 1.5. now its giving me error that adminmedia is not valid library. kindly help me how to solve my problem – numerah Jun 26 '13 at 11:35
  • You should probably move that very last paragraph directly to the top of your answer. – moooeeeep Mar 31 '14 at 14:15
  • Shouldn't you use the standard form Media class instead of hacking the template manually? – Cerin Jan 13 '16 at 21:06
  • You're right that it is easier/safer to use a 3rd party JS date/time picker, but there are many to choose from. I suggest [flatpickr](https://flatpickr.js.org/) as it exists as a standalone package and is very simple to set up. – Thismatters Aug 04 '20 at 12:27
  • Doesn't use of the `AdminSplitDateTime` widget require using `SplitDateTimeField`? Using this solution, I get the error described in https://stackoverflow.com/q/58932440 (this answer was written a long time ago, so maybe this is only an issue for Django >= 2) – Tony S Yu Dec 17 '20 at 15:27
67

As the solution is hackish, I think using your own date/time widget with some JavaScript is more feasible.

zgoda
  • 12,775
  • 4
  • 37
  • 46
  • 8
    I agree. I tried doing this a few months ago with my django project. I eventually gave up and just added my own with jQueryUI. Took all of 10 minutes to get it working. – priestc Jun 15 '09 at 01:33
  • 8
    For the record, I agree, I've upvoted this answer, and I've never actually used the technique in my answer in a production site ;-) – Carl Meyer May 12 '10 at 19:01
14

I find myself referencing this post a lot, and found that the documentation defines a slightly less hacky way to override default widgets.

(No need to override the ModelForm's __init__ method)

However, you still need to wire your JS and CSS appropriately as Carl mentions.

forms.py

from django import forms
from my_app.models import Product
from django.contrib.admin import widgets                                       


class ProductForm(forms.ModelForm):
    mydate = forms.DateField(widget=widgets.AdminDateWidget)
    mytime = forms.TimeField(widget=widgets.AdminTimeWidget)
    mydatetime = forms.SplitDateTimeField(widget=widgets.AdminSplitDateTime)

    class Meta:
        model = Product

Reference Field Types to find the default form fields.

Nick Retallack
  • 18,986
  • 17
  • 92
  • 114
monkut
  • 42,176
  • 24
  • 124
  • 155
  • 5
    I disagree that this approach is less hackish. It looks nicer, sure, but you are completely overriding the form fields generated by the model form, which could also erase options from the model fields, like help_text. Overriding __init__ only changes the widget and leaves the rest of the form field as it was. – Carl Meyer Dec 22 '09 at 23:26
13

My head code for 1.4 version(some new and some removed)

{% block extrahead %}

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/global.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/widgets.css"/>

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/actions.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/calendar.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/admin/DateTimeShortcuts.js"></script>

{% endblock %}
Romamo
  • 131
  • 1
  • 2
  • 3
    awesome . i was trying this from past 2 weeks and this help me thanks alot – numerah Apr 23 '13 at 12:53
  • using {{ STATIC_URL }} or {% load staticfiles %} with {% static 'admin/js/core.js' %}... is required for Django 1.4+, as the whole MEDIA_URL namespace was deprecated and removed. the /admin/jsi18n resolves properly if you have ^admin/ mapped in server/urls.py – jeberle Aug 30 '13 at 18:09
  • nice stuff, useful in the case of Django 1.4 – Ashish Sep 10 '14 at 18:32
12

Yep, I ended up overriding the /admin/jsi18n/ url.

Here's what I added in my urls.py. Make sure it's above the /admin/ url

    (r'^admin/jsi18n', i18n_javascript),

And here is the i18n_javascript function I created.

from django.contrib import admin
def i18n_javascript(request):
  return admin.site.i18n_javascript(request)
  • Oh, very good point. I'll add this to my answer above so it's easier for people to find before they have trouble. – Carl Meyer Jan 10 '09 at 23:03
10

Starting in Django 1.2 RC1, if you're using the Django admin date picker widge trick, the following has to be added to your template, or you'll see the calendar icon url being referenced through "/missing-admin-media-prefix/".

{% load adminmedia %} /* At the top of the template. */

/* In the head section of the template. */
<script type="text/javascript">
window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";
</script>
mshafrir
  • 5,190
  • 12
  • 43
  • 56
8

For Django >= 2.0

Note: Using admin widgets for date-time fields is not a good idea as admin style-sheets can conflict with your site style-sheets in case you are using bootstrap or any other CSS frameworks. If you are building your site on bootstrap use my bootstrap-datepicker widget django-bootstrap-datepicker-plus.

Step 1: Add javascript-catalog URL to your project's (not app's) urls.py file.

from django.views.i18n import JavaScriptCatalog

urlpatterns = [
    path('jsi18n', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]

Step 2: Add required JavaScript/CSS resources to your template.

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
<script type="text/javascript" src="{% static '/admin/js/core.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static '/admin/css/widgets.css' %}">
<style>.calendar>table>caption{caption-side:unset}</style><!--caption fix for bootstrap4-->
{{ form.media }}        {# Form required JS and CSS #}

Step 3: Use admin widgets for date-time input fields in your forms.py.

from django.contrib.admin import widgets
from .models import Product

class ProductCreateForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name', 'publish_date', 'publish_time', 'publish_datetime']
        widgets = {
            'publish_date': widgets.AdminDateWidget,
            'publish_time': widgets.AdminTimeWidget,
            'publish_datetime': widgets.AdminSplitDateTime,
        }
Munim Munna
  • 17,178
  • 6
  • 29
  • 58
  • As described in the other answer, another answer (https://stackoverflow.com/a/1392329) I believe the use of `AdminSplitDateTime` requires the use of `SplitDateTimeField`---otherwise, you'll run into issues when submitting (https://stackoverflow.com/q/58932440/) – Tony S Yu Dec 17 '20 at 15:32
7

Complementing the answer by Carl Meyer, I would like to comment that you need to put that header in some valid block (inside the header) within your template.

{% block extra_head %}

<link rel="stylesheet" type="text/css" href="/media/admin/css/forms.css"/>
<link rel="stylesheet" type="text/css" href="/media/admin/css/base.css"/>
<link rel="stylesheet" type="text/css" href="/media/admin/css/global.css"/>
<link rel="stylesheet" type="text/css" href="/media/admin/css/widgets.css"/>

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/media/admin/js/core.js"></script>
<script type="text/javascript" src="/media/admin/js/admin/RelatedObjectLookups.js"></script>

{{ form.media }}

{% endblock %}
Alex. S.
  • 143,260
  • 19
  • 55
  • 62
5

The below will also work as a last resort if the above failed

class PaymentsForm(forms.ModelForm):
    class Meta:
        model = Payments

    def __init__(self, *args, **kwargs):
        super(PaymentsForm, self).__init__(*args, **kwargs)
        self.fields['date'].widget = SelectDateWidget()

Same as

class PaymentsForm(forms.ModelForm):
    date = forms.DateField(widget=SelectDateWidget())

    class Meta:
        model = Payments

put this in your forms.py from django.forms.extras.widgets import SelectDateWidget

cezar
  • 11,616
  • 6
  • 48
  • 84
Dennis Kioko
  • 741
  • 4
  • 13
  • 27
5

Another simple solution for Django 3 (3.2) in 2021 ;) cause andyw's solution doesn't work in Firefox...

{% load static %}

{% block extrahead %}
{{ block.super }}
<script type="text/javascript" src="{% static 'admin/js/cancel.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}">
<script src="{% url 'admin:jsi18n' %}"></script>
<script src="{% static 'admin/js/jquery.init.js' %}"></script>
<script src="{% static 'admin/js/core.js' %}"></script>
{{ form.media }}
{% endblock %}

<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Сохранить">
</form>

and you'll able to use your form. example:

from django.contrib.admin import widgets
date_time = forms.SplitDateTimeField(widget=widgets.AdminSplitDateTime)
rrretry
  • 103
  • 1
  • 7
4

Here's another 2020 solution, inspired by @Sandeep's. Using the MinimalSplitDateTimeMultiWidget found in this gist, in our Form as below, we can use modern browser date and time selectors (via eg 'type': 'date'). We don't need any JS.

class EditAssessmentBaseForm(forms.ModelForm):
    class Meta:
        model = Assessment
        fields = '__all__'

    begin = DateTimeField(widget=MinimalSplitDateTimeMultiWidget())

enter image description here

andyw
  • 3,505
  • 2
  • 30
  • 44
  • AttributeError: 'NoneType' object has no attribute 'copy' Do you have working app example? – ABHAY KOTAL Nov 20 '20 at 15:15
  • I initialized it using this code: date_created = forms.DateTimeField(widget=MinimalSplitDateTimeMultiWidget(attrs={'type': 'date'})) But it did not work – ARKhan Dec 07 '20 at 09:15
  • you need to provide more info @ARKhan – andyw Dec 07 '20 at 09:47
  • @ABHAYKOTAL @ARKhan replace the code with this ` date_attrs = attrs or {} time_attrs = attrs or {}` – Dan Sto Domingo Feb 22 '21 at 13:25
  • I updated the gist to reflect @DanieleStoDomingo's fix – andyw Feb 22 '21 at 14:23
  • This works great in Apr 2021. Easiest way to get what you need – Anupam Apr 12 '21 at 05:53
  • Thanks for pointing this out. Although browser support for `` is not ideal yet, it is quite good for `"date"` and `"time"` types, according to [this](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#browser_compatibility). – djvg Sep 08 '21 at 17:39
  • An updated solution - https://stackoverflow.com/a/70800722/182604 – spookylukey Jan 21 '22 at 11:46
3

What about just assigning a class to your widget and then binding that class to the JQuery datepicker?

Django forms.py:

class MyForm(forms.ModelForm):

  class Meta:
    model = MyModel

  def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    self.fields['my_date_field'].widget.attrs['class'] = 'datepicker'

And some JavaScript for the template:

  $(".datepicker").datepicker();
trubliphone
  • 4,132
  • 3
  • 42
  • 66
  • http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform – trubliphone Feb 09 '12 at 17:41
1

Updated solution and workaround for SplitDateTime with required=False:

forms.py

from django import forms

class SplitDateTimeJSField(forms.SplitDateTimeField):
    def __init__(self, *args, **kwargs):
        super(SplitDateTimeJSField, self).__init__(*args, **kwargs)
        self.widget.widgets[0].attrs = {'class': 'vDateField'}
        self.widget.widgets[1].attrs = {'class': 'vTimeField'}  


class AnyFormOrModelForm(forms.Form):
    date = forms.DateField(widget=forms.TextInput(attrs={'class':'vDateField'}))
    time = forms.TimeField(widget=forms.TextInput(attrs={'class':'vTimeField'}))
    timestamp = SplitDateTimeJSField(required=False,)

form.html

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/admin_media/js/core.js"></script>
<script type="text/javascript" src="/admin_media/js/calendar.js"></script>
<script type="text/javascript" src="/admin_media/js/admin/DateTimeShortcuts.js"></script>

urls.py

(r'^admin/jsi18n/', 'django.views.i18n.javascript_catalog'),
Sam A.
  • 131
  • 2
  • 4
  • I have also filled a ticket to fix the SplitDateTime widgets http://code.djangoproject.com/ticket/12303 – Sam A. Dec 02 '09 at 15:07
1

I use this, it's great, but I have 2 problems with the template:

  1. I see the calendar icons twice for every filed in template.
  2. And for TimeField I have 'Enter a valid date.'

    Here is a screenshot of the Form

models.py

from django.db import models
    name=models.CharField(max_length=100)
    create_date=models.DateField(blank=True)
    start_time=models.TimeField(blank=False)
    end_time=models.TimeField(blank=False)

forms.py

from django import forms
from .models import Guide
from django.contrib.admin import widgets

class GuideForm(forms.ModelForm):
    start_time = forms.DateField(widget=widgets.AdminTimeWidget)
    end_time = forms.DateField(widget=widgets.AdminTimeWidget)
    create_date = forms.DateField(widget=widgets.AdminDateWidget)
    class Meta:
        model=Guide
        fields=['name','categorie','thumb']
zx485
  • 28,498
  • 28
  • 50
  • 59
potemkin
  • 31
  • 4
0

In Django 10. myproject/urls.py: at the beginning of urlpatterns

  from django.views.i18n import JavaScriptCatalog

urlpatterns = [
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
.
.
.]

In my template.html:

{% load staticfiles %}

    <script src="{% static "js/jquery-2.2.3.min.js" %}"></script>
    <script src="{% static "js/bootstrap.min.js" %}"></script>
    {# Loading internazionalization for js #}
    {% load i18n admin_modify %}
    <script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/jquery.init.js" %}"></script>

    <link rel="stylesheet" type="text/css" href="{% static "/admin/css/base.css" %}">
    <link rel="stylesheet" type="text/css" href="{% static "/admin/css/forms.css" %}">
    <link rel="stylesheet" type="text/css" href="{% static "/admin/css/login.css" %}">
    <link rel="stylesheet" type="text/css" href="{% static "/admin/css/widgets.css" %}">



    <script type="text/javascript" src="{% static "/admin/js/core.js" %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/SelectFilter2.js" %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/admin/RelatedObjectLookups.js" %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/actions.js" %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/calendar.js" %}"></script>
    <script type="text/javascript" src="{% static "/admin/js/admin/DateTimeShortcuts.js" %}"></script>
0

My Django Setup : 1.11 Bootstrap: 3.3.7

Since none of the answers are completely clear, I am sharing my template code which presents no errors at all.

Top Half of template:

{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block head %}
    <title>Add Interview</title>
{% endblock %}

{% block content %}

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<script type="text/javascript" src="{% static 'js/jquery.js' %}"></script>

Bottom Half:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/actions.min.js' %}"></script>
{% endblock %}
Arindam Roychowdhury
  • 5,927
  • 5
  • 55
  • 63
0

June 3, 2020 (All answers didn't worked, you can try this solution I used. Just for TimeField)

Use simple Charfield for time fields (start and end in this example) in forms.

forms.py

we can use Form or ModelForm here.

class TimeSlotForm(forms.ModelForm):
    start = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'HH:MM'}))
    end = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'HH:MM'}))

    class Meta:
        model = TimeSlots
        fields = ('start', 'end', 'provider')

Convert string input into time object in views.

import datetime
def slots():
    if request.method == 'POST':
        form = create_form(request.POST)
        if form.is_valid():                
            slot = form.save(commit=False)
            start = form.cleaned_data['start']
            end = form.cleaned_data['end']
            start = datetime.datetime.strptime(start, '%H:%M').time()
            end = datetime.datetime.strptime(end, '%H:%M').time()
            slot.start = start
            slot.end = end
            slot.save()
sandeep
  • 721
  • 1
  • 7
  • 14