0% found this document useful (0 votes)
12 views2 pages

Want To Show Value of All Fields in Every Object Instance of A Model in HTML - Using Django - Getting Started - Django Forum

The document discusses printing the values of all fields for every object instance of a model in Django. The asker wants to print phone number, spending score, and annual income for all customer objects belonging to a particular user. Answers provide guidance on iterating through the queryset and appending objects to a list to pass to the template context.

Uploaded by

ataush sabuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Want To Show Value of All Fields in Every Object Instance of A Model in HTML - Using Django - Getting Started - Django Forum

The document discusses printing the values of all fields for every object instance of a model in Django. The asker wants to print phone number, spending score, and annual income for all customer objects belonging to a particular user. Answers provide guidance on iterating through the queryset and appending objects to a list to pass to the template context.

Uploaded by

ataush sabuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Want to show value of all fields in every object instance… Log In

Using Django Getting Started

Want to show value of all fields in every object instance of a model in html
Using Django Getting Started

Apr 2023

3/5
Apr 2023

Apr 2023

thisisankit27 1 Apr 2023


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!

Solved by thisisankit27 in post #5


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) conte…

created last reply 4 4.2k 2 1


Apr 2023 Apr 2023 replies views users like

KenWhitesell Apr 2023


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 95 .
thisisankit27 Apr 2023
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', 'spen

this is the output of:

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

KenWhitesell Apr 2023


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 7

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.

thisisankit27 Apr 2023


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>

Solution 1

Reply

Related Topics

Topic Replies Views Activity

How can I count the fields of All Objects in a Database Table by date in Django?
10 7.0k Mar 2021
Using Django

Displaying Queryset object as values


3 194 Mar 2023
Forms & APIs

Returning Table Values from User Selection


23 2.8k Aug 2022
Templates & Frontend

Creating a summary of objects


2 889 Apr 2020
Using Django

getting the sum of prices for each user


1 418 Aug 2022
Using the ORM

You might also like