S
S
Sergey Ganzhela2017-07-27 21:44:07
Django
Sergey Ganzhela, 2017-07-27 21:44:07

Django timezone conversion?

Hello!
tell me in what format you need to pass the time to the template in order to use it correctly ! and is it possible to transfer the value of the time zone to the tag?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
clopik, 2017-07-28
@Sergiy_Hanzhela

You need to install the pytz library. And then you can use the following code:

import pytz

from django.utils import timezone
from django.views.generic import TemplateView


class PageView(TemplateView):
    template_name = 'page.html'

    def get_context_data(self, **kwargs):
        context = super(PageView, self).get_context_data(**kwargs)
        context['dt'] = timezone.datetime(2000, 12, 31, 12, 0, 0, tzinfo=pytz.UTC)
        return context

And further in the template:
{% load tz %}

{% timezone "Europe/Paris" %}{{ dt }}{% endtimezone %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question