[Answered ]-How to make a GET request in an extended Django template

1👍

There are few ways:

  1. You could implement a custom template tag
  2. 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.

👤Lewis

0👍

Try a custom template tag.
Steps involved:

  1. Create a directory templatetags at the same level as your view, with an __init__.py
  2. Create a file in templatetags with any name e.g. make_get_request.py
  3. 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
  1. Restart server.
  2. In the template, write {% load make_get_requests %} i.e. the file name.
  3. Use the function in the template, like:
{% get_request %}
👤Sid

Leave a comment