Reputation:
Following this tutorial, I am creating a simple ecommerce website. I have data stored in a Django model, and I am calling it in my view, but it will not display in my html. Basically, I want it to create one of the boxes bellow for every donation in the donation model, but it will not work. Can someone please help me? I know this seems like an easy fix, I just can't wrap my head around it. My code is down bellow.
View:
def availablesupplies(request):
donations = Donation.objects.all()
context = {'donations':donations}
return render(request, 'availablesupplies.html')
Model:
class Donation(models.Model):
title = models.CharField(max_length=30)
phonenumber = models.CharField(max_length=12)
category = models.CharField(max_length=20)
quantity = models.IntegerField(blank=True, null=True,)
location = models.CharField(max_length=50, blank=True, null=True,)
description = models.TextField()
datedonated = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
blank=True,
null=True,
)
HTML:
<div class="row">
{% for donation in donations %}
<div class="col-lg-4">
<img class="thumbnail" src="{% static 'images/gooddeedplaceholderimage.png' %}">
<div class="box-element product">
<h6><strong>Product</strong></h6>
<hr>
<button class="btn btn-outline-secondary add-btn">Add to Cart</button>
<a class="btn btn-outline-success" href="#">View</a>
<h4 style="display: inline-block; float: right"><strong>$20</strong></h4>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
Upvotes: 1
Views: 422
Reputation: 477794
You need to pass the context object to the render engine:
def availablesupplies(request):
donations = Donation.objects.all()
context = {'donations':donations}
# pass the context ↓
return render(request, 'availablesupplies.html', context)
Upvotes: 2