Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime field in Django form model

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>
like image 559
misschoksondik Avatar asked Mar 26 '13 16:03

misschoksondik


People also ask

What is the format of datetime in Django?

And, because the format is dd/mm/yyyy, we must use a slash to separate the date, month and year.

How do I get form fields in Django?

Basically to extract data from a form field of a form, all you have to do is use the form. is_valid() function along with the form. cleaned_data. get() function, passing in the name of the form field into this function as a parameter.

What is CharField in Django?

CharField is a string field, for small- to large-sized strings. It is like a string field in C/C+++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput.

How do I get the current date and time in Django?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.


2 Answers

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.

like image 195
Udi Avatar answered Sep 29 '22 10:09

Udi


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.

like image 35
fakhir hanif Avatar answered Oct 02 '22 10:10

fakhir hanif