rochdi bouslama
rochdi bouslama

Reputation: 123

Display users in django

I'd like to display users coming from sqlite3 database in html page with django, I wrote the code but it didn't work

views.py

def showUsers(request):
    conn = sqlite3.connect('testDB')
    c = conn.cursor()
    c.execute("SELECT * FROM person_user where role='simpleuser' ;")
    rows = c.fetchall()
    return render_to_response("all-users.html",{'users':rows,'role':request.session["role"],'username':request.session["username"]})

allusers.html

{% for user in users%}
            <div class="col-lg-4">
                <div class="form_hover " style="background-color: #428BCA;">
                <p style="text-align: center; margin-top: 20px;">
                    <i class="fa fa-user" style="font-size: 147px;"></i>
                </p>
                <div class="header">
                    <div class="blur"></div>
                    <div class="header-text">
                    <div class="panel panel-success" style="height: 247px;">
                        <div class="panel-heading">
                            <h3 style="color: #428BCA;">{{user.username}} Details</h3>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                Name : <b>{{user.username}}</b>
                            </div>
                        <div class="form-group">
                                Role :<b>{{user.role}}</b>
                            </div>
                        </div>
                    </div>
                    </div>
                </div>
                </div>
            </div>
            {% endfor %}

Upvotes: 1

Views: 68

Answers (1)

catavaran
catavaran

Reputation: 45595

c.fetchall() returns list of tuples so you can't get the field value by it's column name.

To solve this issue use this recipe from the django documentation:

def dictfetchall(cursor):
    desc = cursor.description
    return [dict(zip([col[0] for col in desc], row))
            for row in cursor.fetchall()]

def showUsers(request):
    ...
    rows = dictfetchall(c)
    ...

Upvotes: 1

Related Questions