0

I have a block called car_table and two kind of cars: blue and red. I want a template with two tables, one for the red cars and one for the blue cars, but I want to use just the 'general' block car_table.

in base_template.html I define the headers and general stuff

<!DOCTYPE html>
 blablabla...
 {% block content %}{% endblock %}
</html>

here is cars_table.html

{% extends base_template.html %}
{% block content %}
 blablabla...
 {% block table %}
 ...
 {% for car in cars %}
 <tr>
 <td>{{ car.name }}</td>
 </tr>
 {% endfor %}
 {% endblock %}
{% endblock %}

Now, I want a page with two tables: blue cars and red cars, using just the code in cars_table

asked Jan 15, 2020 at 8:44
3
  • how about having one template (both_tables.html) and the template with the actual table would be included (via {% include car_table.html %}) twice, each for one colour of cars, instead of extending the base template Commented Jan 15, 2020 at 8:50
  • How can I specify the variables on which perform the loops? One should be on red_cars and the other on blue_cars, instead in car_table.html I have only a loop on cars. Commented Jan 15, 2020 at 9:02
  • 1
    {% with red_cars as cars_for_loop %} {% include car_table.html %}{% endwith %} Commented Jan 15, 2020 at 9:51

1 Answer 1

1

reposting the comments: How about having one template (eg. both_tables.html) and the template with the actual table would be included (via {% include %}) twice, each for one colour of cars, instead of extending the base template:

both_tables.html:

{% with red_cars as cars %}
 {% include car_table.html %}
{% endwith %}
{% with blue_cars as cars %}
 {% include car_table.html %}
{% endwith %}

car_table.html:

{% for car in cars %}
 <tr>
 <td>{{ car.name }}</td>
 </tr>
{% endfor %}
answered Jan 15, 2020 at 9:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.