0

I've just made use of the example shown in the Django doco of setting the active timezone : https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#selecting-the-current-time-zone .

The thing is I the template shown there renders with nothing in the Select box . Everything else seems fine .

Here's my version of it:

    {% 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>

It all renders fine but the Select box is empty - Can anyone explain that ?


EDITS IN RESPONSE TO COMMENTS

My view code looks like this :

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

I can confirm that pytz is installed. pip freeze includes :

pytz==2015.4

With respect to "where did I define timezones ?" That's sort of my question. I don't understand why the code in the doco suggests 'timezones' is just there . I wondered if it was a side effect of {% load tz %} but otherwise ... yes that's the nub of my question.


EDITS IN RESPONSE TO COMMENTS

@Anentropic: when you say I'm missing an endfor ... that's not my code that's out the Django doco. Maybe it's missing an endfor but if it is I can't see where the endfor should be.

For the record here's the actual template code I'm using (which is a cut and paste from that Django code)

    <!-- 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>
glaucon
  • 8,112
  • 9
  • 41
  • 63
  • `{% for tz in timezones %}` => where did you define `timezones`??? – François Constant Jul 27 '15 at 12:14
  • show your view code... if you copied the example from the docs exactly then you should have list of timezones from `pytz` module, I would expect a 500 error if you haven't `pip install pytz` yet though. also you're missing the `{% endfor %}` in the template, which should also raise an error – Anentropic Jul 27 '15 at 12:43
  • @François: thanks for your response. I'm about to edit the question to respond to your point. – glaucon Jul 27 '15 at 21:33
  • @Anentropic: thanks for your response. I'm about to edit the question to respond to your point – glaucon Jul 27 '15 at 21:33
  • OK so I've provided edits to the question which I hope address the comments to date. – glaucon Jul 27 '15 at 21:47
  • re the `{% endfor %}` it's not missing after all... I didn't see it as you've lost a line break in pasting to SO and I had to scroll all the way to the right to see it :) – Anentropic Jul 27 '15 at 22:32
  • 1
    re where the `timezones` variable is defined, there's no mystery magic, you are passing it in as template context in the `render` method when you do this `{'timezones': pytz.common_timezones}`... and the `{% load tz %}` in the template is what makes the `get_current_timezone` templatetag available on the next line – Anentropic Jul 27 '15 at 22:34
  • I don't really see any reason it shouldn't work... you could try putting `

    {{ timezones }}

    ` somewhere prominent in your template and report back what is printed out
    – Anentropic Jul 27 '15 at 22:37
  • @Anentropic: Thanks your comment about setting the value in the render method reminded me of what my problem was. In fact I was using a CBV that already had a get_context_data method so I supplied the value there and, bam, it works ! Thank you very much for your help. Would you mind pasting your comment into an answer and then I can mark it as correct ? Thanks. – glaucon Jul 28 '15 at 01:32
  • ah, that would have been obvious if you'd posted your actual view code... `def set_timezone(request)` looks like a function view, but it sounds like you're not using it – Anentropic Jul 28 '15 at 08:31

0 Answers0