How to Connect to and Print Out Data from a Database Table in Django



Python


In this article, we show how to connect to a database table and print out data from the database table in Django.

So, basically, what we want to do on this page is connect to the database and, through the view.py file, render HTML showing the data from the database table.

So, we're assuming you've already created the database table, added some rows of data to it, and made all necessary migrations so that this database table is synched with the entire project directory.

From this point, all that we have to do is connect to the database and show the results.

To do this, we deal strictly with the views.py file.

So open up the views.py file in the app that you created the database table that you want to show results for.

Before we do this, for my personal site, I created a database table called Students. I created this table, Students, in the Articles app.

This Students table has 3 columns: firstname, lastname, and email address. And of course, it has an id attribute (or primary key) because Django always creates a primary key column to act as a unique identifier for a row of data.

We are going to obtain these 4 pieces of data in our code.

We show the following code in the views.py file for the app that has the database table. Then we'll go over each line of it.



So, since we are outputting HTML through the HTTPResponse function, we have to import HttpResponse from django.http

The next line is the important line for connecting to the database table(s) that we want to connect to.

To connect to the database table, we must import the database table into the view.py file. Without this, we cannot have connection to the database table. This is the crucial line.

Since database are created in the models.py file, you import the database from models and you specify the name of the database table that you want to import. In this case, we are importing the table, Students, so we specify import Students. You remove Students and put the name of the database table that you want to import.

So, that is how we establish connection with the table of a database.

So, we have established connection right now. Now, we just have to retrieve data from the table and print out, so that it can be seen on the website.

So, we create a function called index and pass in as a parameter, request, which must always be passed in. The function is called because in the urls.py file in the App, we list view.index in the urlpatterns.

In this function we create a variable called allstudents and set it equal to Students.objects.all()

If you were just to put this variable allstudents into the HttpResponse() function, it wouldn't work because this code creates a Queryset. The HttpResponse, with HTML tags, only accepts strings. Therefore, an error would be thrown if we simply passed in allstudents into the HttpResponse() function. We cannot do this. Therefore, we create another variable called html and set it equal initially to an empty string.

We then create a for loop that loops through all allstudents.

We want to retrieve the student's id attribute, first name, last name, and email.

We set html equal to += to add on these attributes. Therefore, html will contain all of the rows in the table.

We then return HttpResponse function with h1 header tags.

I have 2 rows of students within my database table, Students.

So running the above code yields for me the following page.

Database connection and data display in Django

This shows all of the results in the Students database table in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...