Django: Displaying MySQL table records

Django Django MySQL Database

Connecting to

To execute our model and display records we must add our function to file.
from django.urls import include, path
from.import views

urlpatterns = [
When we use above url, our url page inside student app will run using display function and returns display.html ( see the below code for

To display records from MySQL database we will create one model first. If you have checked our Django MySQL tutorial their we have already created one class student. Here it is again.
from django.db import models
# Create your models here.
class student(models.Model):
    name =models.CharField(max_length=100)
    mark =models.IntegerField()
    gender  =models.CharField(max_length=6)
    class Meta:
In the above code we have created one student class and declared all fields with field types.

Inside the we will import the above class student and define the function display. Here is the code inside
from django.shortcuts import render
from .models import student
# Create your views here.

def display(request):
	st=student.objects.all() # Collect all records from table 
	return render(request,'display.html',{'st':st})
In above code inside we have declared display function and inside the function we returned display.html page.

In second line we have imported student class from file by saying from .models import student

Note the line saying st=student.objects.all() , this collects all records from the table. It returns a list ( variable st ) with all data. We are creating a dictionary and passing the values to our display.html page.
Here display.html page will receive the data and display them. Here is the code inside display.html
{% for sts in st %}

{{}},{{}},{{}}, {{sts.mark}}, {{sts.gender}}
{% endfor %}
This will display all records of student table from our database. Here are few records of output.
1,John Deo,Four, 75, female
2,Max Ruin,Three, 85, male
3,Arnold,Three, 55, male
4,Krish Star,Four, 60, female
5,John Mike,Four, 60, female
Inside our file the display() we can change like this to show 10 records in the order of id ( increasing ) , Read more about ORDER BY query.
For decresing order of id
35,Rows Noump,Six, 88, female
34,Gain Toe,Seven, 69, male
33,Kenn Rein,Six, 96, female
32,Binn Rott,Seven, 90, female
31,Marry Toeey,Four, 88, male
30,Reppy Red,Six, 79, female
29,Tess Played,Seven, 55, male
28,Rojj Base,Seven, 86, female
27,Big Nose,Three, 81, female
26,Crelea,Seven, 79, male
From 6th to 10th record.
6,Alex John,Four, 55, male
7,My John Rob,Five, 78, male
8,Asruid,Five, 85, male
9,Tes Qry,Six, 78, male
10,Big John,Four, 55, female
Matching string using filter(), here name column is checked.
3,Arnold,Three, 55, male
Similarly we can match id
st=student.objects.filter(id = 30)
30,Reppy Red,Six, 79, female
Name column starting with A
3,Arnold,Three, 55, male
6,Alex John,Four, 55, male
8,Asruid,Five, 85, male

Using less than greater than and equal to

By using Mark column we can get the matching rows based on conditions. Here mark column is more than or equal to 90. ( mark >=90 )

gte : Greater than or equal to ( >= )
gt : Greater than (>)
lte : Less than or equal to (<=)
lt : Less than (<)
st=student.objects.filter(mark__gte=90) # >= 90
12,Recky,Six, 94, female
32,Binn Rott,Seven, 90, female
33,Kenn Rein,Six, 96, female
st=student.objects.filter(mark__gt=90) # > 90
12,Recky,Six, 94, female
33,Kenn Rein,Six, 96, female
st=student.objects.filter(mark__lte=55) #<=55
3,Arnold,Three, 55, male
6,Alex John,Four, 55, male
10,Big John,Four, 55, female
17,Tumyu,Six, 54, male
19,Tinny,Nine, 18, male
22,Reggid,Seven, 55, female
29,Tess Played,Seven, 55, male
st=student.objects.filter(mark__lt=55) # < 55
17,Tumyu,Six, 54, male
19,Tinny,Nine, 18, male

Collecting single record

Using get() we will collect single record and display the same without using any looping.
def display(request):
    return render(request,'display.html',{'st':st})
Here we have to change the html file as we are not going to loop through any QuerySet. ( Error message : 'student' object is not iterable )


List with row details

Using above knowledge let us create one list showing name of students and on click of the name the full details of the student like id, name, class, mark ,gender will be displayed.
Listing Students with link to details

Web form data inserting to MySQL table
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    how to display only one record....can you tell me


    how to display only one record....can you tell me pls


    I am not getting the class student on the html page.

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer