List Info

Thread: Re: How to create a nested list in a view?




Re: How to create a nested list in a view?
country flaguser name
United States
2007-09-30 22:37:57
You are right - you don't want to do all that in the view. 
You should 
be able to pass in your railway_list as an object to the
template then 
in your template show each item as {{railway_list.name}} or
something 
like that. 

Dan wrote:
> Hello All
>
> I seem to be having a bit of a problem with separation
of markup and
> code.
>
> I would like to output from 3 different models;
railways, rail lines
> and stations a nested unordered list, so far i have got
this to work:
>
> def view_transport(request):
>     railway_list = Railway.objects.all()
>     output = '<ul>'
>     for rail in railway_list:
>         output +=
'<li>'+rail.name+'</li><ul>'
>         line_list = Line.objects.filter(railway=rail)
>         for line in line_list:
>             output +=
'<li>'+line.name+'</li><ul>'
>             station_list =
> Station.objects.filter(line=line)
>             for station in station_list:
>                 output +=
'<li>'+station.name+'</li>'
>             output += '</ul>'
>             output += '</ul>'
>     output += '</ul>'
>     return render_to_response('map/transport.html',
{'var': output})
>
> This will output something like this:
>
> <ul><li>Tokyo
Metro</li><ul><li>Ginza
Line</li><ul><li>Shibuya</li></
> ul></ul><li>Japan
Railway</li><ul></ul>
>
> This is fine for the output but I think there should be
a better way
> to achieve this without having any markup in the view,
i have seen the
> {{ var|unordered_list }} template tag but I do not have
a clue how I
> would create a nested list in the view to pass to this
template tag.
>
> I am a first time user of Python and Django, any help
with this would
> be greatly appreciated, thanks in advance.
>
> -Dan
>
>
> >
>
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )