Django时区示例 - 为什么我没有获得任何时区?

时间:2015-07-27 11:52:27

标签: django timezone django-timezone

我刚刚使用了设置活动时区的Django doco中显示的示例:https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#selecting-the-current-time-zone

问题是我显示的模板在选择框中没有任何内容呈现。其他一切似乎都很好。

这是我的版本:

    {% 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="selected"{% endif %}>{{ tz }}</option>                                                                                                    {% endfor %}
        </select>
        <input type="submit" value="Set" />
    </form>

这一切都很好,但是选择框是空的 - 任何人都可以解释一下吗?

对评论作出反应的编辑

我的观看代码如下所示:

来自django.shortcuts导入重定向,渲染

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})

我可以确认安装了pytz。 pip freeze包括:

pytz==2015.4

关于&#34;我在哪里定义了时区?&#34;这就是我的问题。我不明白为什么doco中的代码暗示了时区&#39;就在那里。我想知道这是否是{% load tz %}的副作用,但是否则......是的,这是我问题的核心。

对评论作出反应的编辑

@Anentropic:当你说我错过了一个结尾......那不是我的代码,而是Django doco。也许它错过了一个结局,但如果是的话,我无法看到最终的结果。

这里的记录是我使用的实际模板代码(来自该Django代码的剪切和粘贴)

    <!-- The set TZ form -->
    {% 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="selected"{% endif %}>{{ tz }}</option>
            {% endfor %}
        </select>
        <input type="submit" value="Set" />
    </form>

0 个答案:

没有答案