Python 如何获取当前用户时区?德扬戈

Python 如何获取当前用户时区?德扬戈,python,django,Python,Django,我正在尝试让我的Django应用程序与当前用户时区一起工作,但没有得到任何结果 在my settings.py文件中,我设置: 时区='UTC' 在我的views.py中,我在代码顶部设置: 激活(设置时区) 但这不起作用,虽然创建了一个帖子,但却得到了我的最后一个时区“马德里”,而不是我的当前时区 有没有办法让这一切顺利进行 谢谢大家! 来自 从 这就是原因 将以下中间件添加到中间件: import pytz from django.utils import timezone class

我正在尝试让我的Django应用程序与当前用户时区一起工作,但没有得到任何结果

在my settings.py文件中,我设置:
时区='UTC'

在我的
views.py
中,我在代码顶部设置:

激活(设置时区)

但这不起作用,虽然创建了一个帖子,但却得到了我的最后一个时区“马德里”,而不是我的当前时区

有没有办法让这一切顺利进行

谢谢大家!

来自


这就是原因

将以下中间件添加到中间件:

import pytz

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)
创建可设置当前时区的视图:

from django.shortcuts import redirect, render

def set_timezone(request):
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect('/')
    else:
        return render(request, 'template.html', {'timezones': pytz.common_timezones})
在template.html中包含将发布到此视图的表单:

{% load tz %}
{% get_current_timezone as TIME_ZONE %}
<form action="{% url 'set_timezone' %}" method="POST">
    {% csrf_token %}
    <label for="timezone">Time zone:</label>
    <select name="timezone">
        {% for tz in timezones %}
        <option value="{{ tz }}"{% if tz == TIME_ZONE %} selected{% endif %}>{{ tz }}</option>
        {% endfor %}
    </select>
    <input type="submit" value="Set">
</form>

在{%localtime%}块中不考虑USE_TZ的值

时区 设置或取消设置包含块中的当前时区。当前时区未设置时,将应用默认时区

{% load tz %}

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

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}
获取当前时区 您可以使用get_current_timezone标记获取当前时区的名称:

{% get_current_timezone as TIME_ZONE %}

或者,您可以激活tz()上下文处理器,并使用时区上下文变量。

以下是该变量的来源

将以下中间件添加到中间件:

import pytz

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)
创建可设置当前时区的视图:

from django.shortcuts import redirect, render

def set_timezone(request):
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect('/')
    else:
        return render(request, 'template.html', {'timezones': pytz.common_timezones})
在template.html中包含将发布到此视图的表单:

{% load tz %}
{% get_current_timezone as TIME_ZONE %}
<form action="{% url 'set_timezone' %}" method="POST">
    {% csrf_token %}
    <label for="timezone">Time zone:</label>
    <select name="timezone">
        {% for tz in timezones %}
        <option value="{{ tz }}"{% if tz == TIME_ZONE %} selected{% endif %}>{{ tz }}</option>
        {% endfor %}
    </select>
    <input type="submit" value="Set">
</form>

在{%localtime%}块中不考虑USE_TZ的值

时区 设置或取消设置包含块中的当前时区。当前时区未设置时,将应用默认时区

{% load tz %}

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

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}
获取当前时区 您可以使用get_current_timezone标记获取当前时区的名称:

{% get_current_timezone as TIME_ZONE %}

或者,您可以激活tz()上下文处理器并使用时区上下文变量。

是否可以添加一些信息和代码?您是否在您的模型中使用django.utils import timezone和
timezone.now()
或类似的
models.DateTimeField(auto\u now\u add=True)
。因此django将使用该时区作为默认时区,然后在视图中激活该(相同)时区。所以这显然不会有什么不同。django的本地化可能相当复杂,但要从时区文档开始;你能添加一些信息和代码吗?您是否在您的模型中使用django.utils import timezone和
timezone.now()
或类似的
models.DateTimeField(auto\u now\u add=True)
。因此django将使用该时区作为默认时区,然后在视图中激活该(相同)时区。所以这显然不会有什么不同。django的本地化可能相当复杂,但要从时区文档开始;