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)
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.
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.
{% 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
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
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 ){{st.id}},{{st.name}},{{st.cl}},{{st.mark}},{{st.gender}}
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. |