I am trying to get the values for a query:
total_sales = (Model.query
.with_entities(Model.dept_name,
sa.func.sum(Model.sales),
sa.func.count(Model.sales))
.filter(Model.companyid == companyid)
.filter(Model.sales> 0)
.group_by(Model.companyid, Model.dept_name)
.all())
In my jinja code I have:
{% for item in total_sales %}
<tr>
<td>{{ item.dept_name}}</td>
</tr>
<tr>
<td>{{ item.sum }}</td>
</tr>
<tr>
<td>{{ item.count }}</td>
</tr>
{% endfor %}
The dept_name shows up but the item.sum isn't showing and the item.count shows up as <built-in method count of result object at 0x000002DEA910D098>
If I print(total_sales) I can see the results of the query as a list of tuples. How do I access this in my jinja file?
You need to add labels to the function results so you can access them more clearly. Since you are using count and sum in the template, it'd be like:
sa.func.sum(Model.sales).label('sum'),
sa.func.count(Model.sales).label('count')
Related
I am trying to display data on my web app from a CSV file using Flask. In my code below, I am trying to select 5 columns from my CSV file and display the values of these columns in an HTML table. Unfortunately, the values that appear in the HTML table are {{ value [0] }}, {, {{ value [1] }}, { value [2] }}, {{ value [3] }}, {{ value [4] }}, {{ value [5] }}.
My python function:
#app.route("/homepage")
def table_issues():
columns=["Regulatory_Domain","Detection_Date","ID_Client","Issue_ID_Name","Status_Issue","Comments"]
df = pd.read_csv("data.csv", names=columns, header= 0)
df = df.sort_values(by=['Detection_Date'], ascending = True)
issueslist = list (df.values)
return render_template("homepage.html", issueslist=issueslist)
My HTML code:
<table>
<thead class=theadissues>
<tr>
<th>Regulatory Domain</th>
<th>Detection Date</th>
<th>Client-ID</th>
<th>Issue ID / Name</th>
<th>Review Status</th>
<th>Comments</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ value[0] }}</td>
<td>{{ value[1] }}</td>
<td>{{ value[2] }}</td>
<td>{{ value[3] }}</td>
<td>{{ value[4] }}</td>
<td>{{ value[5] }}</td>
<td>
<p class="EditAction">EDIT ISSUE</p>
</tr>
</tbody>
</table>
Could you please help me to correct my code above.
Many thanks in advance for your help.
change the word 'value' to 'issueslist'
when you rendered the template you passed it a list...
return render_template("homepage.html", issueslist=issueslist)
So now your html template has access to that same variable
<td>{{ issueslist[0] }}</td>
My Django View:
def hub(request):
context = {}
hub_id = [value['id'] for value in hub_data['data']]
hub_name = [value['attributes']['name'] for value in hub_data['data']]
hub_url = [value['links']['self']['href'] for value in hub_data['data']]
nested_dict = dict(zip(hub_name, map(list, zip(hub_id, hub_url))))
context ['rows'] = nested_dict
return render(request, 'connector/hub.html', context)
The context['rows'] results in:
{'rows': {hub_name1 : ['hub_id1', 'hub_url1'],{hub_name2 : ['hub_id2', 'hub_url2'], etc.. }
I am trying to pass it a HTML table that looks like this:
<th scope="col">Hub Name</th>
<th scope="col">Hub ID</th>
<th scope="col">Hub URL</th>
My tablebody looks like this:
<tbody>
{% for key, value in rows.items %}
<tr>
<td> {{ key }}</td>
<td> {{ value }}</td> **//how do i just get hub_id here**
<td> Don't know what to do here to get: hub_url </td>
</tr>
{% endfor %}
</tbody>
But I want to add another -tag to fill with hub_url. How do I extract the hub_id data and add it to the column: Hub ID and extract the hub_url and add it to the column Hub URL.
Any help would be much appreciated!
You can pass the data to the template without transforming it
return render(request, 'connector/hub.html', {'data': hub_data['data']})
And then lookup the attributes using the "dot" template syntax for each row
<tbody>
{% for row in data %}
<tr>
<td>{{ row.attributes.name }}</td>
<td>{{ row.id }}</td>
<td>{{ row.links.self.href }}</td>
</tr>
{% endfor %}
</tbody>
I have two lists in python that I would like to input into two separate columns in an HTML table. What would the code look like on the python end of things and what would it look like for html?
This is what I have in python now:
giver = []
recipient = []
giver.append(name)
recipient.append(match)
return render_template("created.html", givers=givers, recpients=recipients)
And this is what I have in HTML:
<table>
<thead>
<tr>
<th>Giver</th>
<th>Recipient</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ givers }}</td>
<td>{{ recipients }}</td>
</tr>
</tbody>
</table>
Create one list
data = []
data.append( (name, match) )
return render_template("created.html", data=data)
and use it in template
{% for row in data %}
<tr>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
</tr>
{% endfor %}
Obligatory "I am new to Django" here...
In my views I am creating a list, called records_list. Inside that list, I have another list in the position [0] and a dictionary in the position [1], as follows:
records_list = list()
list_one = Bet.objects.order_by('-game_date')
list_two = {}
Inside the "list_two", that is my dictionary, I have a key that is a date "April 2016", for ex, and a value that is a tuple:
list_two[aux_month_year] = (aux_wins, aux_losses, aux_voids, s_rate, i_rate, profit)
So I return this to my html:
records_list.append(list_one)
records_list.append(list_two)
return records_list
In the html, I want to create a table, and I start by checking if my profit is positive or not:
{% if records_list %}
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Wins</th>
<th>Losses</th>
<th>Void</th>
<th>Success Rate</th>
<th>Return on Investment</th>
<th>Profit</th>
</tr>
</thead>
{% for key in records_list.1 %}
{% if ((records_list.1.key.5) > 0) %}
<tr class="success">
<td>{{ key }}</td>
<td>{{ records_list.1.key.0 }}</td>
<td>{{ records_list.1.key.1 }}</td>
<td>{{ records_list.1.key.2 }}</td>
<td>{{ records_list.1.key.3 }}%</td>
<td>{{ records_list.1.key.4 }}%</td>
<td>{{ records_list.1.key.5 }}</td>
</tr>
However, I am getting the following syntax error here:
{% if ((records_list.1.key.5) > 0) %}
Error:
Could not parse the remainder: '((records_list.1.key.5)' from '((records_list.1.key.5)'
If someone could help me and point me to the right direction I would appreciate!
Thank you
The if tag does not support parentheses. From the docs:
Use of actual parentheses in the if tag is invalid syntax. If you need them to indicate precedence, you should use nested if tags.
In your case, the parentheses are not required, so just remove them:
{% if records_list.1.key.5 > 0 %}
Basically, what I want to do is have the template system loop through two independent lists to fill two columns of a table. My approach was to use an index list (numList) as a way to access the same index of the two lists. I tried using the dot notation for list index lookup within a template loop but it doesn't seem to work in the loop. Any ideas on how I can remedy this?
numList = [0, 1, 2, 3]
placeList = ['park', 'store', 'home', 'school']
speakerList = ['bill', 'john', 'jake', 'tony']
<table>
<tr>
<th>Location</th>
<th>Time</th>
<th>Speaker</th>
</tr>
{% for num in numList %}
<tr>
<td>{{ placeList.num }}</td>
<td>1:30</td>
<td>{{ speakerList.num }}</td>
</tr>
{% endfor %}
</table>
The easiest thing is probably to combine your lists in python and then just look through the combined list in the template:
combinedList = [(placeList[i],speakerList[i]) for i in range(4)]
{% for entry in combinedList %}
<tr>
<td>{{ entry.0 }}</td>
<td>1:30</td>
<td>{{ entry.1 }}</td>
</tr>
{% endfor %}
Or for transparency, you could make combinedList a list of objects or dictionaries for example:
combinedList = [{'place':placeList[i],'speaker':speakerList[i]} for i in range(4)]
{% for entry in combinedList %}
<tr>
<td>{{ entry.place }}</td>
<td>1:30</td>
<td>{{ entry.speaker }}</td>
</tr>
{% endfor %}
You could aggregate these two lists in one.
For example:
yourlist = [('park','bill'),('store','john'),('home','jake'),...]