1👍
✅
There are few ways:
- You could implement a custom template tag
- You could write your own custom context processor and inject it into every rendered templateenter link description here
Each approach has its own tradeoffs. Since you’re trying to display dynamic data, watch out for your own caching or hitting too many external services that would slow down rendering (so that you don’t make it blocking).
0👍
Create a template context processor as seen in This answer
You will then want to render this into your base.html
in order to apply to all templates in your application.
- [Answered ]-How Can I Sort These Many To Many Values Without a Through Table?
- [Answered ]-How to hide django default filters from others except admin user?
0👍
Try a custom template tag.
Steps involved:
- Create a directory
templatetags
at the same level as your view, with an__init__.py
- Create a file in
templatetags
with any name e.g.make_get_request.py
- In that file, enter something like this:
from django import template
register = template.Library()
@register.filter(name = 'get_request')
def get_request():
#make your request here
#return a list, dict etc. with the info you need
- Restart server.
- In the template, write
{% load make_get_requests %}
i.e. the file name. - Use the function in the template, like:
{% get_request %}
👤Sid
- [Answered ]-Heroku and Django connection pool
- [Answered ]-403 error for new files posted through django admin
- [Answered ]-Django get all objects whose many to many filed contains a filter
- [Answered ]-Django field to select range between two values?
Source:stackexchange.com