Im trying to create a section in my application which contains a loop of catagories, and then the forms within that category. For example it should display like this:
Category 1
form 1
form 2
Category 2
form 3
But what im actualy getting is:
Category 1
form 1
form 2
form 3
Category 2
form 1
form 2
form 3
How can i fix this?
My view is:
def homepage (request):
Categories = Category.objects.all()
Forms = Form.objects.all()
output = {
'Category_Name': Categories,
'Form_Title': Forms,
}
return render_to_response('forms/home.html', RequestContext(request, output))
And my HTML is:
<ul>{% for c in Category_Name %}<li>{{ c.Name }}<ul>{% for c in Form_Title %}
<li><a href="/forms/{{ c.id }}">{{ c.Title }}</a></li>{% endfor %}</ul></li>{% endfor %}</ul>
if your forms are associated to a category via a foreignkey
you could do something like this:
take a look at the foreignkey documentation.
also note its not a good idea to use the same variable (c) in the outer and the inner loop,even if it works like in your example
and in python variable names are usually written lowercase