views:

52

answers:

3

Hey Guys,

I'm having problems displaying nested blocks in a template.

eg.

   {% for category in categories %}

         //code to display category info 

         {% products = products.object.filter(category = category) %}
         {% for product in products%}
              //code to display product info
         {% endfor %}
   {% endfor %}

I'm getting a "Invalid block tag: 'endfor'" error.

Any ideas?

A: 

I think you cannot use arguemnts for methods. You have to modify your categories object, so that you kann use:

{% for product in category.products %}
Sven Walter
A: 
{% products = products.object.filter(category = category) %}

is not recognized as a valid tag in the django template system. Therefore django complains about the missing endfor, although the {% for x in y %) is not the error.

This should work

 {% for category in categories %}
     {% for product in products.object.all %}
         //code to display product info
     {% endfor %}
 {% endfor %}

But this is not that, what you want to achieve. Simply you are not able to filter on product.objects with the argument category.

You have to write your own tag which takes arguments on filtering or rethink your problem.

zovision
I've abstracted the filtering into a method within categories: eg category.get_products so now my inner loop reads: {% products = category.get_products %} {% for product in products%} //code to display product info {% endfor %} Still getting the same error?
Philip
A: 

You cannot assign to variables in the Django template system. Your two attempts:

{% products = products.object.filter(category = category) %}

and

{% products = category.get_products %}

are both invalid Django syntax.

Some Python templating systems are PHP-like: they let you embed Python code into HTML files. Django doesn't work this way. Django defines its own simplified syntax, and that syntax does not include assignment.

You can do this:

{% for category in categories %}

     //code to display category info 
     {% for product in category.get_products %}
          //code to display product info
     {% endfor %}
{% endfor %}
Ned Batchelder