Want To Show Value of All Fields in Every Object Instance of A Model in HTML - Using Django - Getting Started - Django Forum
Want To Show Value of All Fields in Every Object Instance of A Model in HTML - Using Django - Getting Started - Django Forum
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
models.py:
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:
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>
<QuerySet [{'id': 1, 'vendor_id': 2, 'phoneNumber': '1', 'spendingScore': 39, 'annualIncome': 15}, {'id': 2, 'vendor_id': 2, 'phoneNumber': '2', 'spen
customerDataList = CustomerBill.objects.filter(
vendor=request.user).values()
print(customerDataList)
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.
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
How can I count the fields of All Objects in a Database Table by date in Django?
10 7.0k Mar 2021
Using Django