How to Print Out All Rows of a Database Table with Python in Django



Python


In this article, we show how to print out all rows of a database table with Python in Django.

In this example, we have a database table and we're going to show how to print out all rows and columns of the database table so that it can be displayed to the user.

So let's get right to it.


models.py File

So we're going to create a database table called Video.

It's a database table composed of videos.

The code for this database table is shown below.



It has 3 fields, name, description, and the video itself.

The name field contains the name of the video.

The description field is the video description.

The videofile field is the field through which a user uploads a video.

And that's all.


views.py File

Next we go to the views.py file.

Into the views.py file, we import this database table, Video.

We then create a variable that holds all of the objects in the database table.

Lastly, we pass this variable into the context dictionary and render it to a template file.

This is shown in the code below.



So let's go through all of this code.

We import render from django.shorcuts, so that we can render the template file, which in this case is home.html.

We have to import the database table we want to print out, in this case, is Video.

We then create a function-based view, showvideo()

We create a variable, allvideos, and set it equal to, Video.objects.all()

This gets all of the objects (rows) in the database table.

So this one line is the main part of our code. It gets all of the objects from the database table, Video. All rows are now contained in this variable, allvideos.

We then create a context dictionary and pass in the variable, allvideos, into it.

We then render the home.html template file, passing in the context dictionary.


template File

Lastly, we'll now show how to display all of this data.

There's many forms we could display the data in, such as ordered lists, unordered lists, tables, etc.

In this example, we'll display the data out in a table (HTML table).

So below is the contents of the template file we are rendering.



So let's now go over this code.

So we have our HTML tag and our head tag.

We'll now go to the body.

In the body we have our, h1 tag, List of Videos.

We then have a table. Since we want a border around the table, we specify a border="1". And we just give it a width, so that it isn't full screen.

We then create a header row, which consists of the name of each column of the table.

We then create a for loop (template for loop) to loop through each object of the database table.

Since the name of the video is in the name column, we reference this by the statement, {{ video.name }}

Since the video description is in the description column, we reference this by the statement, {{ video.description }}

The last column is the videofile column. This contains the path to the video, excluding the media directory which precedes this path.

In this column, we play the video through the HTML video tag.

In the src attribute of the video, we specify, src='{{ MEDIA_URL }}{{ video.videofile }}

video.videofile references the path to the video file we are playing through the HTML video player.

We then close the rows.

And then we end the for loop.

Outside of this for loop, we close the table tag.

Just so that you can see the output this produces, first just know that I put in 2 rows into this table field.

The admin of this database table can be seen here: Video table example in Django.

After running the above code, this is what I got as output, below.

Print out of table of all rows of a database table wtih Python in Django



And this is all that is required to print out all rows of a database table with Python in Django.


Related Resources

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django

How to Create a File Uploader with Python in Django



HTML Comment Box is loading comments...