Maurice Elagu
Maurice Elagu

Reputation: 704

Django raw query not returning/rendering results on html template

Am currently working on an application where am sapposed to access static a database of another app. These two applications are independent but share the same database. I am sapposed to perform some raw sql(postgrres to be exact) queries in my views since am bypassing the model class. But no results are returned on my template here is view and template code

#views code

def receipt(request):
cursor = connection.cursor()

# Data retrieval operation

    cursor.execute("SELECT * FROM quickpay_client_transactions")
    transactions = cursor.fetchall()

return render(request, 'receipt.html', {"transactions":transactions})


#Templates code snippet

{% for trans in transactions %}
                                            <tr>
                                                <td>
                                                {{trans.id}}    
                                                </td>
                                                <td></td>
                                                <td>{{trans.amount}}</td>
                                                  <th>{{trans.reference}}</th>
                                                <td>
                                                        <span class="label label-success">Success</span>
                                                </td>
                                                <th>{{trans.t_stamp}}</th>
                                            </tr>
                                            {% endfor %}

Upvotes: 0

Views: 1383

Answers (1)

Avinash Garg
Avinash Garg

Reputation: 1402

first check you are getting any result in

transactions = cursor.fetchall()

second type of transactions should be list

sample working code with dummy data:

from django.shortcuts import render
def index(request):

    transactions = [{"id":1},{"id":2}]
    return render (request, 'test/index.html', {"transactions":transactions})

html:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
Test
{% for trans in transactions %}
                                            <tr>
                                                <td>
                                                {{trans.id}}
                                                </td>

                                            </tr>
                                            {% endfor %}
</body>
</html>

or may be your sql data is not in str: try this

transactions = [to_string(x) for x in cursor.fetchall()]
import datetime
import decimal
def to_string(x):
    a = []
    for y in x:
        if type(y) is datetime.datetime:
            a.append(str(y))
        elif type(y) is decimal.Decimal:
            a.append(str(y))
        elif type(y) is unicode:
            a.append(y.encode("UTF-8", "ignore"))
        elif type(y) is float:
            a.append(str(y))
        else:
            a.append(y)
    return a

Upvotes: 3

Related Questions