Reputation: 241
I am trying to print some database values onto an HTML page.
The html code is run through a for loop that counts the amount of description values.
However it prints the entire database for each entry of Debit , Credit and Account Number.
I'm pretty sure the problem is inside the for loop structure , please assist.
Home.html:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
{% extends "main/base.html"%}
{% block content%}
<h1> Kyle Database </h1>
<h2>Trial Balance</h2>
<br>
<br>
<table>
<th>Account</th>
<th>Description</th>
<th>Debit</th>
<th>Credit</th>
{% for xAlls in xAll %}
<tr>
<td>{{ accountNo }}</td>
<td>{{ description }}</td>
<td>{{ debit }}</td>
<td>{{ credit }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
Views.py:
def home(request):
return render(request , 'main/home.html')
def Kyletrb(request):
desc = "SELECT Description FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor = cnxn.cursor();
cursor.execute(desc);
description = [tup[0] for tup in cursor.fetchall()]
accNo = "SELECT Account FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(accNo);
accountNo = [tup[0] for tup in cursor.fetchall()]
deb = "SELECT Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(deb);
debit = [tup[0] for tup in cursor.fetchall()]
cred = "SELECT Credit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(cred);
credit = [tup[0] for tup in cursor.fetchall()]
all = "SELECT Description, Account ,Credit,Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(all);
xAll = [tup[0] for tup in cursor.fetchall()]
return render(request , 'main/Kyletrb.html' , {"description":description , "accountNo":accountNo , "debit":debit , "credit":credit , "xAll":xAll})
Upvotes: 1
Views: 2235
Reputation: 130
First of all, you can make a simple query for all data at once ( as you did in the last fetch ). I would make a list of dicts like this:
def Kyletrb(request):
all = "SELECT Description, Account ,Credit,Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(all);
xAll = cursor.fetchall()
cursor.close()
xAll_l = []
for row in xAll:
rdict = {}
rdict["Description"] = row[0]
rdict["Account"] = row[1]
rdict["Credit"] = row[2]
rdict["Debit"] = row[3]
xAll_l.append(rdict)
return render(request , 'main/Kyletrb.html' , {"xAlls":xAll_l})
After that you can make a for loop in template:
<table>
<th>Account</th>
<th>Description</th>
<th>Debit</th>
<th>Credit</th>
{% for xAll in xAlls %}
<tr>
<td>{{ xAll.Description }}</td>
<td>{{ xAll.Account }}</td>
<td>{{ xAll.Debit }}</td>
<td>{{ xAll.Credit }}</td>
</tr>
{% endfor %}
</table>
Upvotes: 1
Reputation: 241
I have figured out the formatting problem .
Instead of using a table, I have used 'div' tags for each column
.html :
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
{% extends "main/base.html"%}
{% block content%}
<h1>Kyle Database Trial Balance</h1>
<br>
<div class="container">
<div class="row mb-0">
<div class="col">
<h3>Account</h3>
{% for accountNo in accountNo %}
<p style="font-size:10px">{{ accountNo }}</p>
{% endfor %}
</div>
<div class="col-4">
<h3>Description</h3>
{% for description in description %}
<p style="font-size:10px">{{ description }}</p>
{% endfor %}
</div>
<div class="col">
<h3>Debit</h3>
{% for debit in debit %}
<p style="font-size:10px">{{ debit }}</p>
{% endfor %}
</div>
<div class="col">
<h3>Credit</h3>
{% for credit in credit %}
<p style="font-size:10px">{{ credit }}</p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
Upvotes: 0
Reputation: 1
Please try as below.
{% for description in descriptions %}
<tr>
<td>{{ description.accountNo }}</td>
<td>{{ description.description }}</td>
<td>{{ description.debit }}</td>
<td>{{ description.credit }}</td>
</tr>
{% endfor %}
Upvotes: 0
Reputation: 123
You can replace your for loop with the following code. and I hope it will work.
{% for desc in description %}
<tr>
<td>{{ desc.accountNo }}</td>
<td>{{ desc.description }}</td>
<td>{{ desc.debit }}</td>
<td>{{ desc.credit }}</td>
</tr>
{% endfor %}
and make sure that you have same objects of models given in your HTML template
Upvotes: 0
Reputation: 2086
You are using the same variable name in your for loop twice.
Normally it would be
for(description in descriptions)
Notice the plurality difference.
Upvotes: 0
Reputation: 58
Try this code
{% for description in description %}
<tr>
<td>{{ description.accountNo }}</td>
<td>{{ description.description }}</td>
<td>{{ description.debit }}</td>
<td>{{ description.credit }}</td>
</tr>
{% endfor %}
Upvotes: 0