R
R
Rrooom2014-09-08 13:40:02
JavaScript
Rrooom, 2014-09-08 13:40:02

How to work with dates in Django?

Somehow I constantly come across errors with dates. Especially with js widgets that want to format them in their own way.
Is there material that explains how to avoid mistakes?
Two complaints - something that regularly breaks with an error - "timezone not specified" (temporarily disabled its check in the settings).
The second is that python and js format dates differently, these ***** localized formats, in which format is everything guaranteed to be fine?
How to filter by date? It would be nice for us to know the exact datetime of the records, but the end user will have enough for days. Because of this, not pretty formatting for dates - 00:00:00 - part with time.
In general, help me understand - in the official documentation there are no examples at all about this bunch of rakes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2014-09-08
@Kademn

The problem is not entirely clear. In the view, you form data using the capabilities of python, then in any order and format you give it to the template. In a template, you can represent datetime in a form convenient for you, for example.
So you determine the "inconvenient format for the user" yourself, and you will do the JavaScript processing based on how you submit data in the template.
Look here
+
In whatever form you pass data to the form, you can override the data validation for the form yourself.
Read more here

G
Gasoid, 2014-09-08
@Gasoid

Difficulty only with time zone yes.
Otherwise, it is not there, if you just have date,
create a form with the DateField field, the
Russian date format is perfectly validated, i.e. standard jquery ui datepicker puts 09/08/2014, this format is known by django forms

if forms.valid():
    date = forms.cleaned_data['date']
   # в date уже правильный для питона Date объект
   # дальше фильтруем по дате, например объявления, которые позже даты
   ads = Ad.objects.filter(date__gt=date)

# все есть в документации языка запросов

M
Maxim Vasiliev, 2014-09-08
@qmax

already looked here?
https://docs.djangoproject.com/en/1.7/topics/i18n/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question