Python Django Django MySQL Our Listing page output is here Alex John My John Rob Asruid Tes Qry Big John On click of the link we will get output like this. 7,My John Rob,Five,78,maleBack Connecting to urls.py To execute our model and display records we must add our function to urls.py file. urls.pyfrom django.urls import include, pathfrom.import viewsurlpatterns = [ 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. views.py def display(request): id=request.GET.get('id', 'Not available') if(id != 'Not available'): st=student.objects.get(id=id) return render(request,'details.html',{'st':st}) else: st=student.objects.all().order_by('id')[5:10] return render(request,'list.html',{'st':st}) Here we can see if the value of id is not available ( id='Not available' ) then list.html is used and if id value is available then details.html is used.
Learn how to collect query string data here. We used st=student.objects.all().order_by('id')[5:10] to generate one QuerySet with records from id 6 to 10, this line can be changed to get different set of records by using filters.
Different filters and string matching for getting QuerySet. list.html {% extends 'temp_basic.html' %}{% block content %}{% for sts in st %} <a href=?id={{sts.id}}>{{sts.name}}</a><br>{% endfor %}{% endblock %}
details.html
{{st.id}},{{st.name}},{{st.cl}},{{st.mark}},{{st.gender}}<br><a href=./>Back</a> Web form data inserting to MySQL table