Re: stop repetition in template
actually i want both.. like LIST OF ALL COLLEGES: college1 city1 college2 city2 college3 city3 LIST OF CITIES city1 city2 city3 {but is city2 = city3} then it would show : LIST OF ALL COLLEGES: college1 city1 college2 city2 college3 city3 LIST OF CITIES city1 city2 -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: stop repetition in template
I like your second solution a lot better than mine. I've never seen {% ifchanged %} used properly before. Brian On Wed, May 4, 2011 at 1:19 PM, Javier Guerra Giraldez wrote: > On Wed, May 4, 2011 at 11:42 AM, pankaj sharma > wrote: > > in template.. > > > > {% for college in list %} > >{{college.city}} > > {% endfor %} > > > > in views.py > > > > def list(request): > >college_list=College.objects.all() > >return render_to_response( > >'college/list.html', > >{'list':college_list} > >) > > > > > > > > so suppose i have two colleges in one city so it is showing the city > > name twice .. so how do i stop repetition.. > > two different solutions, depending on what do you want: > > A) if you only need the cities from a given college queryset, in the > view do something like: > > render_to_response( > 'college/list.html', >{'cities_list':City.objects.filter(college_set__in = college_list)} > ) > > B) if you want a list of colleges, separated by city, and only show > the city once for each group of colleges: > > in the view: > > return render_to_response( > 'college/list.html', >{'list':college_list.order_by('city')} > ) > > in the template: > > {% for college in list %} > {% ifchanged %}{{college.city}}{% endifchanged %} > {{college.name}} > {% endfor %} > > > -- > Javier > > -- > 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: stop repetition in template
On Wed, May 4, 2011 at 11:42 AM, pankaj sharma wrote: > in template.. > > {% for college in list %} > {{college.city}} > {% endfor %} > > in views.py > > def list(request): > college_list=College.objects.all() > return render_to_response( > 'college/list.html', > {'list':college_list} > ) > > > > so suppose i have two colleges in one city so it is showing the city > name twice .. so how do i stop repetition.. two different solutions, depending on what do you want: A) if you only need the cities from a given college queryset, in the view do something like: render_to_response( 'college/list.html', {'cities_list':City.objects.filter(college_set__in = college_list)} ) B) if you want a list of colleges, separated by city, and only show the city once for each group of colleges: in the view: return render_to_response( 'college/list.html', {'list':college_list.order_by('city')} ) in the template: {% for college in list %} {% ifchanged %}{{college.city}}{% endifchanged %} {{college.name}} {% endfor %} -- Javier -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: stop repetition in template
I would handle this at the views layer. Right now your queryset of college_list is a list of colleges containing cities. In your view code I would re-organize it to be a list of cities that contain colleges. Then you can iterate through cities and get colleges. You can build this data structure dynamically in the view code without explicitly pre-defining it as a class or model. Hope this helps. Brian On Wed, May 4, 2011 at 12:42 PM, pankaj sharma wrote: > hello friends, > i have database of colleges. > i want to show all cities > > so what is did is.. > in template.. > > {% for college in list %} >{{college.city}} > {% endfor %} > > in views.py > > def list(request): >college_list=College.objects.all() >return render_to_response( >'college/list.html', >{'list':college_list} >) > > > > so suppose i have two colleges in one city so it is showing the city > name twice .. so how do i stop repetition.. > > -- > 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- Brian Bouterse ITng Services -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
stop repetition in template
hello friends, i have database of colleges. i want to show all cities so what is did is.. in template.. {% for college in list %} {{college.city}} {% endfor %} in views.py def list(request): college_list=College.objects.all() return render_to_response( 'college/list.html', {'list':college_list} ) so suppose i have two colleges in one city so it is showing the city name twice .. so how do i stop repetition.. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.