W
W
Wayne2020-01-02 17:28:11
Django
Wayne, 2020-01-02 17:28:11

How to fix date error?

in the parser, I get the date from the site, but it looks like this 14:52 01/02/2020, and I need to translate it into this 02-01-2020 14:52
I did it like this:

date = content_div.find('div', attrs={'class': 'article__info-date'}).find('a').text
date = datetime.datetime.strptime(date, '%H:%M %d.%m.%Y').strftime('%Y-%m-%d %H:%M')

then I add these dates to the model, but for some objects I get an error:
RuntimeWarning: DateTimeField News.date received a naive datetime (2020-01-02 16:39:00) while time zone support is active.
  RuntimeWarning)

Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-01-02
@alexkom6

That "02-01-2020 14:52" does not contain information about the time zone. You need to either set USE_TZ = Falseor disable this setting in settings.py locally, only for this section of code, or set the time zone to the timestamp stored in date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question