简体   繁体   English

DJANGO不同时区显示

[英]DJANGO Different Timezone Display

Django Beginner here. Django初学者在这里。 I'm trying to display different timezones in my template but it's not displaying. 我正在尝试在模板中显示不同的时区,但未显示。

HTML File HTML文件

  {% load tz %}

  {% timezone "Europe/Paris" %}
      Paris time: {{ value }}
  {% endtimezone %}

  {% timezone None %}
      Server time: {{ value }}
  {% endtimezone %}

OUTPUT 输出值

No Display in HTML

In setting.py change TIME_ZONE : 在setting.py中更改TIME_ZONE

TIME_ZONE = 'Europe/Paris'

views.py should look like this: views.py应该看起来像这样:

from django.shortcuts import render
import datetime
from django.utils import timezone

def index(request):
    france = timezone.localtime(timezone.now())
    now = datetime.datetime.now()

    return render(request, 'home/home.html', {'now':now, 'france': france} )

And home.html should look like this: 而且home.html应该看起来像这样:

France time: {{france}}
Server time: {{ now }}

It should work 它应该工作

I'm guessing you're not getting anything displayed because you haven't passed any context to your template. 我猜您没有显示任何内容,因为您没有向模板传递任何上下文。 You would do something like this: 您将执行以下操作:

    from django.utils import timezone
    ...
    return render(request, template, context={'value': timezone.now()})

Another option is to use django's built-in {% now &} with whatever format you want. 另一种选择是将django内置的{% now &}与所需的格式一起使用。

Similar question: Django - present current date and time in template 相似的问题: Django-在模板中显示当前日期和时间

Now docs: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#now 现在的文档: https : //docs.djangoproject.com/en/2.0/ref/templates/builtins/#now

Date filters to include with now: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#std:templatefilter-date 现在要包括的日期过滤器: https : //docs.djangoproject.com/en/2.0/ref/templates/builtins/#std : templatefilter- date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM