11

When using DateTimeField in ModelForms, they look like text fields. How can I make them look like in the admin? (When I go to the admin and add a show I see the fields as date fields)

# models.py
class Show(models.Model):
    ...
    start_time = models.DateTimeField("Event Time")
    sale_end_time = models.DateTimeField("Sale End Time")

class ShowForm(ModelForm):

    class Meta:
        model = Show 

# views.py
def createshow(request):
    if request.method == 'POST':
        form = ShowForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/showsaved')
    else:
        form = ShowForm()
        return render(request, 'BizCreateShow.html', {'ShowForm' : form})

In the template:

<form class="form-horizontal well" action="" method="post">
 {% csrf_token %}
    {{ ShowForm }} </br>
    <input type="submit" value="Submit">
</form>
0

2 Answers 2

9

You can do this with django widgets. It is very simple and easy to implement. Bellow are the details how you can use this using widgets.

in forms.py

from django.contrib.admin import widgets

class ShowForm(ModelForm):
    class Meta:
        model = Show 
    def __init__(self, *args, **kwargs):
        super(ShowForm, self).__init__(*args, **kwargs)
        self.fields['start_time'].widget = widgets.AdminSplitDateTime()
        self.fields['sale_end_time'].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>

<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 is it. Please let me know if i am not much clear. Or you are facing any error in it.

3
  • Thanks, when I try that I get an error: "Undefined variable: widgets" on this line: self.fields['start_time'].widget = widgets.AdminSplitDateTime() Commented Mar 29, 2013 at 10:51
  • 4
    try this "from django.contrib.admin import widgets" may it will solve your problem. Commented Apr 1, 2013 at 14:25
  • 1
    Note that in 1.4+ you call the static files differently: stackoverflow.com/a/15292176/1863061
    – Laci
    Commented Jun 28, 2016 at 11:30
3

You should probably use something like jQuery UI's datepicker widget:

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.

4
  • Thanks! but how can I give the input field a class or ID to use in the javascript if the form generates in the model? Commented Mar 29, 2013 at 11:17
  • Also- I need the time in the same field, don't I? It only lets me select the date. Commented Mar 29, 2013 at 19:30
  • Ok, I used $("form input[name='start_time']").click(function () { $("form input[name='start_time']").datepicker(); }); Commented Mar 31, 2013 at 8:17
  • It needs to be said that this method will NOT work in formsets.
    – addohm
    Commented Dec 28, 2017 at 3:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.