[Django]-Django DateTimeField input Form

7👍

forms.py

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

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['mydate'].widget = widgets.AdminDateWidget()
        self.fields['mytime'].widget = widgets.AdminTimeWidget()
        self.fields['mydatetime'].widget = widgets.AdminSplitDateTime()

In template:

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

…or, for Django 1.4+:

{% load static %}

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

<script type="text/javascript" 
    src="/admin/jsi18n/"></script>
<script type="text/javascript" 
    src="{% static 'admin/js/core.js' %}"></script>
<script type="text/javascript" 
    src="{% static 'admin/js/admin/RelatedObjectLookups.js' %}"></script>
<script type="text/javascript" 
    src="{% static 'admin/js/jquery.js' %}"></script>
<script type="text/javascript" 
    src="{% static 'admin/js/jquery.init.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>

2👍

If you’re stuck with the error

Could not parse the remainder: '/js/jquery.init.js'' from 'admin/js/jquery.init.js''". 

Just add an apstrophe before admin/js/jquery.init.js
it will look like:

'admin/js/jquery.init.js' 
👤lolita

Leave a comment