Haikal Nashuha
Haikal Nashuha

Reputation: 3048

Django Rendering Template Included in a Template

Basically I have a base template, and the base template is including another template, let's say, latest update. So the structure is like this:

-base.html
     |---------latest_update.html

So, I know how to include a simple template, like the template without any data processing (doesn't require interaction with any application view.py). However, how could I attach the included template into a django application view.py so that I could at least show updated data periodically?

I am not exactly sure terms for this, feel free to change the title.

Edit: This question is a bit cloudy, as I don't know how to put the terminology correctly. So, I have this included template. Every page will have it. So, from my limited knowledge, that means I have to render it manually for every page that hits view.py. Is there any easier way of doing this?

Upvotes: 0

Views: 290

Answers (1)

risent
risent

Reputation: 136

You can use Django templatetags. Define a file named latest_update_tags under your Django app templatetags directory and write code like this to define a latest_update templatetags:

from django import template

from app.models import UpdateObject

register = template.Library()
@register.inclusion_tag("latest_update.html")
def latest_update():
  update_objects = UpdateObject.all().[:10]
  return {"update_objects": update_objects}

And then in your base.html use it like this:

{% load latest_update_tags %}
......
{% latest_update %}

Upvotes: 2

Related Questions