zyeek
zyeek

Reputation: 1287

How to create selection tabs for date and time field in Django?

Have looked through DateField, TimeField, DateTimeField related documents but I cannot seem to find what I need. I simply want to have a selection of month, day, year, hour, minute, (AM/PM) type option. I have tried using 'choice=', but do not get the nice behavior I am looking for.

** TL;DR: I simply want a way of putting in the date and time without having to type it in. I would like a nice drop down menu **

class Event(models.Model): 

     event_name  = models.CharField(max_length = 50)    

     date_time   = models.DateTimeField()

     date        = models.DateField()

     location    = models.CharField(max_length = 30)

     address     = models.CharField(max_length = 30)

     city        = models.CharField(max_length = 30)

     zip_code    = models.CharField(max_length = 30)

     state       = models.CharField(max_length = 30)

     description = models.TextField()



     def __unicode__(self):

            return self.event_name



class EventForm(ModelForm):

      class Meta:

         model = Event

This is what I currently have. I have removed the choices part and I even tried making my own model object dedicated to date and time, but that did not go well

I tried it using this ...

DATE_CHOICES = (('Jan', "January"),

            ('Feb', "Feburary"),

            ('Mar', 'March'),

            ('Apr','April'),

            ('May ','May'),

            ('June','June'),

            ('July','July'),

            ('Aug','August'),

            ('Sept','Septemeber'),

            ('Oct','October'),

            ('Nov','November'),

            ('Dec','December')

            )



class DateTime(models.Model):

month = models.CharField(max_length = 5, choices=DATE_CHOICES)

But I am not getting the correct behavior as I want.

Upvotes: 0

Views: 2782

Answers (3)

AlexanderLedovsky
AlexanderLedovsky

Reputation: 747

Your can use datepicker plagin as Erfin mentioned (I recommend it too and also datepicker for bootstrap) for date and masked input for time or just simple selects. Anyway you should send a request with datetime information. What to do in django:

If you use datepicker plugin for date and masked input for time

Let's assume that your request is POST. Your date will be a string with format you specify in javascript. It looks like "31.01.2013". And time will be like "22:30".

def write_datetime(request):
    event = Event.objects.get(id=int(request.POST.get('id')))
    from datetime.datetime import strptime
    date = request.POST.get('date')
    time = request.POST.get('time')
    date_time = date + " " + time
    event.date_time = strptime(date_time, "%d.%m.%y %H:%M"
    event.save()

If you use just selects

In this case, it's simplier to make a datetime string from request parameters and repeat the preveous example.

def def write_datetime(request):
    year = request.POST.get('year')
    month = request.POST.get('month')
    # etc
    date_time = "%s.%s.%s %s:%s" % (day, month, year, hours, minutes)
    # etc        

Upvotes: 0

mariodev
mariodev

Reputation: 15559

You definitely want to use this snippet. Unless you're a django ninja and want to roll up your own multi widget, which is what you will need to transform a set of select inputs into one datetime value.

This widget is the closest you will get to do it, without using any js plugins.

Upvotes: 1

Efrin
Efrin

Reputation: 2423

You might be interested in using jquery date picker or jquery datetime picker.

http://jqueryui.com/datepicker/

http://trentrichardson.com/examples/timepicker/

On both sites there are exampels so you can see it in action :)

Upvotes: 1

Related Questions