Rooterle
Rooterle

Reputation: 393

Django Datetime not saved correctly

I made a calendar in django where I can save events with start and end dates. If I save a new event and enter for example the date '2015-01-11' it saves '2015-01-10'. So everytime I enter a date it is saved in the database -1 day.

here is my model:

class Event(models.Model):
    title = models.CharField(max_length=255)
    start = models.DateTimeField()
    end = models.DateTimeField()

here is my form:

class EventForm(ModelForm):
    class Meta:
        model = Event
        fields = ['title', 'start', 'end']

here I save the new event that is passed via the form

event = Event(
    title=request.POST['title'],
    start=request.POST['start'],
    end=request.POST['end'],
)
event.save()

the date I input in the form is formatted like "%Y-%m-%d" ... for example '2015-01-12'.

Oh and it just affects the start date (not the end date).

Upvotes: 0

Views: 430

Answers (1)

catavaran
catavaran

Reputation: 45595

Set TIME_ZONE setting to match the system time zone.

Upvotes: 1

Related Questions