iterate over alphabet in template

1,163 views
Skip to first unread message

M.Ganesh

unread,
Jun 17, 2008, 12:54:09 PM6/17/08
to django...@googlegroups.com
Hi All,

I am relatively new to both python and django. Please help me to do this :

{% for letter in [A to Z] %} <---- how do I write this line in a template?
<a href=/contacts/entity/?index={{letter}}/>{{letter}}</a>&nbsp;
{% endfor %}


Thanks in advance
Regards Ganesh


Richard Dahl

unread,
Jun 17, 2008, 1:03:22 PM6/17/08
to django...@googlegroups.com
I would probably just pass in a python list with all of the letters in the alphabet, then just
{% for l in alphabet_list %} ...
-richard

Joel Bernstein

unread,
Jun 17, 2008, 1:18:49 PM6/17/08
to Django users
For an easy (but locale-dependent) way to get a list of the uppercase
characters in Python, try string.uppercase

http://www.python.org/doc/2.3/lib/module-string.html

Just add that list to your context, and you should be ready to go.

On Jun 17, 12:03 pm, "Richard Dahl" <rich...@dahl.us> wrote:
> I would probably just pass in a python list with all of the letters in the
> alphabet, then just
> {% for l in alphabet_list %} ...
> -richard
>

M.Ganesh

unread,
Jun 17, 2008, 2:28:48 PM6/17/08
to django...@googlegroups.com
Hi Richard and Joel,

Thanks for your lightning fast replies. I tried this and it worked :

#my view function
return render_to_response(template,
{'object_name' : object.__name__,
'app_label' : object._meta.app_label,
object_list_name : object_list,
'base_template' : base_template,
'alphabet_string' : ascii_uppercase },
RequestContext(request))

#my template
{% for letter in alphabet_string %}
{{ letter }} &nbsp; - &nbsp;
{% endfor %}

Thanks once again

Regards Ganesh

Ned Batchelder

unread,
Jun 17, 2008, 3:30:12 PM6/17/08
to django...@googlegroups.com
If you wanted to keep the alphabet issue out of the view, you could also do this:

{% for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" %}

--Ned.
http://nedbatchelder.com
-- 
Ned Batchelder, http://nedbatchelder.com

M.Ganesh

unread,
Jun 18, 2008, 3:44:26 AM6/18/08
to django...@googlegroups.com
Ned Batchelder wrote:
> If you wanted to keep the alphabet issue out of the view, you could
> also do this:
>
> {% for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" %}
>
> --Ned.
> http://nedbatchelder.com
>
Super. I was just wondering how to do it.

Thanks Ned

Regards Ganesh

Reply all
Reply to author
Forward
0 new messages