Passing context from child to parent template in Django

How do I pass the context used on the child template TO the parent template it inherits from?

Example:

in views.py

def author(request, code):
    author = get_object_or_404(Author, code=code)
    books = Book.objects.filter(author_id=author.id)
    reviews = Review.objects.filter(author_id=author)

    return render(request, 'foo/author.html', {'author': author, 'books': books, 'reviews': reviews})

in author.html

{% extends 'base.html' %}

in base.html

{% for book in books %}
    <ul>
        <li>{{ book.title }}</li>
        <li>{{ book.year_published }}</li>
    </ul>
{% endfor %} 

This is for the navigation bar that's inside the parent template('base.html'). It changes depending on which author page you are on.

Answers


Create a new block for books in child.html. This bookblock on child.html should override the bookblock exists inside base.html

{% bookblock %}
{% for book in books %}
    <ul>
        <li>{{ book.title }}</li>
        <li>{{ book.year_published }}</li>
    </ul>
{% endfor %} 
{% endblock %}

base.html

Put the code related to book inside a block where block name must be same on both base and child templates.

{% bookblock %}
{% for book in books %}
    <ul>
        <li>{{ book.title }}</li>
        <li>{{ book.year_published }}</li>
    </ul>
{% endfor %} 
{% endblock %}

Need Your Help

Python not throwing exception when trying to update an attribute that is not available in the model

exception django-models attributes

Am using python 4.3 and django 1.0.2. When I try to update a field that is not available in the model no exception is raised.

Gecko usage in C# (geckofx)

c# geckofx

There are some things that I didn't find how to do using geckofx: