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>
{{ timezones }}
` somewhere prominent in your template and report back what is printed out – Anentropic Jul 27 '15 at 22:37