I am reading from a flask REST API using python's Requests library and referencing this in a Django view.
The issue i am experiencing is when testing the API call from a Chrome plugin i am seeing a refreshed response where as in the Django template I'm not seeing any change in data when the page is refreshed.
I have a small lib which includes a GET call to the API:
def container_list():
api_auth = get_auth()
server_url = get_server_url() + '/v1/containers'
r = requests.get(server_url , auth = api_auth)
return r.json()
I am then reading this in the view:
from django.views.generic import ListView
from lib.ApiClient import image_list, container_list
from django.core.cache import cache
class ContainerList(ListView):
template_name = 'containers.html'
cache.clear() # Attempt at clearing the cache
queryset=container_list()
This is then served to the template:
{% for container in object_list %}
{% for key,value in container.items %}
{{key}} : {{ value }}
{% endfor %}
{% endfor %}
Any suggestions on how i can get the template to refresh?
You are using ListView
incorrectly. You assign queriset
attribute with something that is not a QuerySet
. This means that you fetch the data only once (at the moment of importing the views.py
).
Every time when the request to the view comes it uses the data in the queryset
attribute. If you want to fetch new data on every request you need to overwrite get_queryset
method. Something like:
class ContainerList(ListView):
template_name = 'containers.html'
def get_queryset(self):
return container_list()
This will fetch the data on every request.