Django: Displaying MySQL table records


Youtube Live session on Tkinter

Django Django MySQL Database

Connecting to urls.py

To execute our model and display records we must add our function to urls.py file.

urls.py
from django.urls import include, path
from.import views

urlpatterns = [
	path('student/display',views.display,name='display'),
]
http://127.0.0.1:8000/student/display
When we use above url, our url page inside student app will run views.py using display function and returns display.html ( see the below code for views.py)

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.

models.py
from django.db import models
# Create your models here.
class student(models.Model):
    name =models.CharField(max_length=100)
    cl=models.CharField(max_length=10,db_column='class')
    mark =models.IntegerField()
    gender  =models.CharField(max_length=6)
    class Meta:
        db_table='student'
In the above code we have created one student class and declared all fields with field types.

Inside the views.py we will import the above class student and define the function display. Here is the code inside views.py
views.py
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 model.py we have declared display function and inside the function we returned display.html page.

In second line we have imported student class from models.py 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.id}},{{sts.name}},{{sts.cl}}, {{sts.mark}}, {{sts.gender}}
<br>
{% 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 views.py file the display() we can change like this to show 10 records in the order of id ( increasing ) , Read more about ORDER BY query.
st=student.objects.all().order_by('id')[:10]
For decresing order of id
st=student.objects.all().order_by('-id')[:10]
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.
st=student.objects.all().order_by('id')[5:10]
output
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.
st=student.objects.filter(name='Arnold')
Output
3,Arnold,Three, 55, male
Similarly we can match id
st=student.objects.filter(id = 30)
Output
30,Reppy Red,Six, 79, female
Name column starting with A
st=student.objects.filter(name__startswith="A")
Output
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):
    st=student.objects.get(id=14)
    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 )

display.html
{{st.id}},{{st.name}},{{st.cl}},{{st.mark}},{{st.gender}}

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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    28-12-2020

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

    26-04-2021

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

    17-02-2022

    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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer