3

I populate the fields for editing an event. The fields are shown as "DD/MM/YY HH:MM:SS", but when I change any of the values - although preserving the format - and try to save it, it shows the error:

Enter a valid date/time

class EventForm(forms.ModelForm):
    ...
    date = forms.DateTimeField(widget=forms.widgets.DateTimeInput(format="%d/%m/%Y %H:%M:%S", attrs={'placeholder':"DD/MM/YY HH:MM:SS"}))
    ...

class Meta:
    model = Event

views.py

def event(request, category_slug, event_slug):
    ...
    event = Event.objects.get(slug=event_slug)
    form = EventForm(instance=my_event)
    context_dict["edit_event_form"] = form
    ...

return render(request, 'handsup/event.html', context=context_dict)

This is how the field is called in event.html:

 <div class="form-group">
     {{ edit_event_form.date.errors }}
     {{ edit_event_form.date.label_tag }}
     {{ edit_event_form.date|addclass:"form-control form-style"}}
</div>

Any comment or suggestion is greatly appreciated. Thank you.

1 Answer 1

3

According to your expression

%d/%m/%Y %H:%M:%S

Your date should contain 4 digits for year, eg: 14/02/2017 10:34:52

If you want to use 2 digits for year, should be with minuscule 'y', something like this:

%d/%m/%y %H:%M:%S

Anyway, I'm not sure if it will be fine for django datetime fields.

Hope it helps

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