In my django web app i wannt show time in user timezone

679 views
Skip to first unread message

Deniz Bazan

unread,
Dec 8, 2018, 1:39:29 AM12/8/18
to Django users
Hello everybody,
I have a django web app, in my app i want to show in the page where the time information is, with user timezone.

I have a form with start_time and end_time(Timefield) user send this with a specific timezone(Berlin/Europa) after this when a user go to the page, the user should see the page with his own timezone.

I use Django 2.1 with postgresql database.

Thanks for any recommendation!
Regards,
Deniz

thanh.ngu...@gmail.com

unread,
Dec 8, 2018, 4:02:33 AM12/8/18
to Django users
Hello, 
I think you can use "request.META" django. (request.META['TZ']). It is timezone user submit request. 

:D 

Vovk Donets

unread,
Dec 8, 2018, 7:02:02 AM12/8/18
to django...@googlegroups.com
From docs: "...there’s no equivalent of the Accept-Language HTTP header that Django could use to determine the user’s time zone automatically. Instead, Django provides time zone selection functions. Use them to build the time zone selection logic that makes sense for you."

There is two paths.
First. You can store time on the server in UTC, detect user timezone and then do conversion to this timezone on your backend. You can also look at https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#localtime
Second path is that you can do time conversion on the frontend using whatever you like JS library to do this work. But in both cases you should determine users timezone yourself or ask user in which TZ he is.

сб, 8 дек. 2018 г. в 07:02, <thanh.ngu...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/eaa47d65-8bd5-4a9d-95a4-28bc5761317c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Vovk Donets
 python developer

Deniz Bazan

unread,
Dec 8, 2018, 4:27:45 PM12/8/18
to django...@googlegroups.com
Hello Vovk Donets,
thanks for your answer.

In my app, i can not ask the user for timezone because its impossible.I send a screen shot from app you can better understand me.

image.png
this is for a Internet TV.The user can be from any timezone.
I want just use django(python) make this possible.
In your answer you say there is two way make this, but i don't understand 1. way(detect user timezone and then do conversion to this timezone on your backend. )


Settings.py
TIME_ZONE = 'UTC'
 
USE_I18N = True
 
USE_L10N = True
 
USE_TZ = True

my model.py
class Guide(models.Model):
    episode=models.ForeignKey(Episode,on_delete=models.CASCADE,verbose_name=_('Program'))
    slug=models.SlugField(max_length=100 ,unique=True,allow_unicode=True)
    create_date=models.DateField(default=timezone.now,verbose_name=_('Publishing Date'))
    start_time=models.TimeField(default=timezone.now,verbose_name=_('Start Time'))
    end_time=models.TimeField(default=timezone.now,verbose_name=_('End Time'))
    publish=models.BooleanField(default=True,verbose_name=_('Publish'))

i use in time field default=timezone.now but i m not sure if should use default=datetime.datetime.now






For more options, visit https://groups.google.com/d/optout.


--
Mit freundlichen Grüßen
Deniz Bazan

Web Entwickler
Filmmaker, Cutter, 3D  Artist
Tel:+49 163 133 68 00
Reply all
Reply to author
Forward
0 new messages