[Answer]-How to handle additional blocks in Django?

1👍

OK,according to your description, I think you can use customized templatetags for the purpose.
Say you want to add recent news in the sidebar (And you have an app called news, while all news stored in a model called Article)

news/templatetags/news.py

from django import template
from django.db.models import get_model
register=template.Library();

@register.simple_tag(takes_context=True)
def get_latest_news(context,context_variable):
    context[context_variable]=get_model('News','Article').objects.order_by('-publish_date')[0,10]
    return ''

base.html

{% load news %}
{% block sidebar %}
    {% get_latest_news 'latest' %}
    {% for one in latest %}
        <p>{{one}}</p>
    {% endfor %}
{% endblock %}

It’s better to start with simple_tag than the complete one as it’s much simpler and fits with most needs.

0👍

maybe you could override generic detail view (django.views.generic.DetailView)

inherit the view class and override get_context_data() will be what you need to do.

👤Ju2017

Leave a comment