Reputation: 317
Im trying to create a table in html but the list is not even working
This is my site.html
{% extends "blog/base.html" %}
{% block content %}
<h1>tab in my website</h1>
<p>site info</p>
{% if result_dict %}
<ul>
{% for i in result_dict %}
<li> {{ forloop.counter }} - {{ i }}</li>
{% endfor %}
</ul>
{% else %}
<p>This is not working</p>
{% endif %}
{% endblock content %}
This what what i have in my views.py
def prtg(request, *args, **kwargs):
response = requests.get("https://prtg.c3ntro.com/api/table.json?content=status=down&username=someuser&passhash=somehash&count=200000000")
data = response.json()
d = data
result_list = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
return render(request, 'blog/prtg.html', {'title':'PRTG'}, {'result_list': result_list})
When I open the page is not loading any information and well it's supposed to show at least the sensors that I'm filtering but nothing shows us, instead I get the message of the else statement "This is not working"
What am I doing wrong?
Upvotes: 0
Views: 184
Reputation: 317
Finally found a way to show what I wanted in html
So the answer was very simple altho I havent found a way to auto refresh this
But the code goes likes this
import requests
import pandas as pd
def example_page(request):
context = {"title" : "example"}
url = "my.website.json"
response = requests.get(url)
data = response.json()
d = data
result_list = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
df = pd.DataFrame(result_list)
return HttpResponse(df.to_html())
That way we get a dataframe built-in html
Altho you must refresh the page to get new data from the json url, got it working tho thanks for your time, ill leave my answer if anyone is having trouble with this
Upvotes: 0
Reputation: 599600
You have two problems. The items need to be in the same dictionary:
return render(request, 'blog/prtg.html', {'title':'PRTG', 'result_list': result_list})
and, you've called it result_list
, not result_dict
, so use that name in the template.
{% if result_list %}
<ul>
{% for i in result_list %}
<li> {{ forloop.counter }} - {{ i }}</li>
{% endfor %}
</ul>
{% else %}
<p>This is not working</p>
{% endif %}
Upvotes: 1