List with link to details from MySQL table
« Django
« Django MySQL Database « Display rows
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,male
Back
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.
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
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com