Want to show value of all fields in every object instance of a model in html

I want to print phoneNumber, spendingScore, annualIncome of all the objects (Customer) under a particular user (Vendor)!

models.py:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class CustomerBill(models.Model):
    vendor = models.ForeignKey(User, on_delete=models.CASCADE)
    phoneNumber = models.CharField(max_length=14, blank=False)
    spendingScore = models.IntegerField()
    annualIncome = models.IntegerField()

    def __str__(self):
        return self.phoneNumber

views.py:

from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages

from . models import CustomerBill

def customerDatabase(request):
    customerDataList = CustomerBill.objects.filter(
        vendor=request.user).values()
    context = {
        'customerData': customerDataList,
    }
    return render(request, 'database.html', context)

html:

<body>
    {% include 'navbar.html' %}
    <br>
    <h2 style="text-align: center;">Customer Database</h2>
    <br>

    <table style="width: 60%; margin: 0 auto;">
        <thead>
            <tr>
                <th>Serial No.</th>
                <th>Phone Number</th>
                <th>Spending Score</th>
                <th>Annual Income</th>
            </tr>
        </thead>
        <tbody>
            {% for i in context %}
            <tr>
                <td>{{forloop.counter}}</td>
                <td>{{i.phoneNumber}}</td>
                <td>{{i.spendingScore}}</td>
                <td>{{i.annualIncome}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</body>

Can anyone help me out? a little urgent!

Have you worked your way through the official Django tutorial? If so, this was covered at Writing your first Django app, part 4 | Django documentation | Django.

I went through it but i wasn’t able to understand how to print list of dictionaries in django template!

<QuerySet [{'id': 1, 'vendor_id': 2, 'phoneNumber': '1', 'spendingScore': 39, 'annualIncome': 15}, {'id': 2, 'vendor_id': 2, 'phoneNumber': '2', 'spendingScore': 81, 'annualIncome': 15},'...(remaining elements truncated)...']>

this is the output of:

customerDataList = CustomerBill.objects.filter(
        vendor=request.user).values()
print(customerDataList)

From the perspective of the template, it doesn’t make any difference whether you have
customerDataList = CustomerBill.objects.filter(vendor=request.user).values()
or
customerDataList = CustomerBill.objects.filter(vendor=request.user)

Review the docs at Variables

Also, do not place too much importance on how you see data represented by printing it. Every object has the ability to define how it’s going to be printed, and such representation may not be directly related to its internal structure.

Thank You for the guidance and quick response @KenWhitesell !

Now it’s fixed, here’s what i did:

views.py:

def customerDatabase(request):
    customerDataList = CustomerBill.objects.filter(
        vendor=request.user)
    temp = []
    for i in customerDataList:
        temp.append(i)

    context = {
        'customerData': temp,
    }
    return render(request, 'database.html', context)

html File:

<tbody>
            {% for itr in customerData %}
            <tr>
                <td>{{forloop.counter}}</td>
                <td>{{itr.phoneNumber}}</td>
                <td>{{itr.spendingScore}}</td>
                <td>{{itr.annualIncome}}</td>
            </tr>
            {% endfor %}
        </tbody>
1 Like