That's fantastic it makes things a lot cleaner thanks for your help, I got it working just by adding '_set' to the associated models like so:
<ul> {% for rail in railway_list %} <li>{{rail.name}}</li> <ul> {% for line in rail.line_set.all %} <li>{{line.name}}</li> <ul> {% for station in line.station_set.all %} <li>{{station.name}}</li> {% endfor %} </ul> {% endfor %} </ul> {% endfor %} </ul> On Oct 1, 4:38 pm, Daniel Roseman <[EMAIL PROTECTED]> wrote: > On Oct 1, 3:28 am, Dan <[EMAIL PROTECTED]> 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 need to use a ForeignKey (or ManyToManyField) in the model > definitions to relate your line, railway and station models together, > rather than doing explicit lookups each time. > > Then in the template you'll be able to do something like this > (untested): > > <ul> > {% for rail in railway%} > <li>{{rail.name}}</li> > <ul> > {% for line in railway.lines.all %} > <li>{{line.name}}</li> > <ul> > {% for station in line.stations.all %} > <li>{{station.name}}</li> > {% endfor %} > </ul> > {% endfor %} > </ul> > {% endfor %} > </ul> > > You may also want to look into the Regroup template tag. > > -- > DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---