I am trying to display data from a website in HTML.
I have created a class called Video in models.py as follows:
class Video(models.Model):
filename = models.CharField("File Name", max_length=100)
title = models.CharField("Video Title", max_length=250)
The database is migrated and I am able to insert data into it using a Django form. I have checked and there is data in the database.
I have the following code in my HTML template file, but the page is empty when it is displayed, and is showing no data.
{% for video in Video %}
<div class="row">
<div class="col-md-3">
<a href="{{ video.filename }}">{{ video.title }}</a>
</div>
</div>
{% endfor %}
Why isn't this displaying any data in the rendered HTML page?
Edit:
This is the relevant code from the view:
def listvideos(request):
"""Renders the listvideos page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/listvideos.html',
)
Templates can only render what you pass to them. You're not sending anything called Video, so you can't iterate over it.
return render(
request,
'app/listvideos.html',
{'Video': Video.objects.all()}
)
You should consider whether you really want to call your parameter Video, though; it should probably be called videos both here and in the template.
(And please remove that assertion. For a start, it has no place in production code; and secondly the request will always be an HttpRequest, that is part of the contract for a view.)