Django Data from Query string


Youtube Live session on Tkinter

Django
Here is our URL ( Query string with single data id )
http://127.0.0.1:8000/my_app/student/my_data/?id=10
We will collect the value of id ( id=10 ) from the URL and display the same.

my_app/urls.py
from django.urls import path 
from  . import views

# Create your views here.
urlpatterns=[
    path('',views.index,name='index'),
    path('student/signup_student',views.signup_student,name='signup_student'),
    path('student/my_data/',views.my_data,name='md'),
    path('student/signup_student_output',views.signup_student_output,name='signup_student_output2'),
    path('student/insert_all',views.insert_all,name='insert_all')
]
my_app/views.py
def my_data(request):
    id=request.GET.get('id','Not available') # collecting value of id
    return render(request,'my_data.html',{'id':id})
We set the path for our template here
settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'smo_temp')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Inside our template directory smo_temp, we kept our data.html file. smo_temp/data.html
{% extends 'temp_basic.html' %}

{% block content %}
Student ID : {{id}}<br>
Student Name : {{name}}

 {% endblock %}

Adding more parameters

We can read more than one parameter from the URL , here along with id=10 we used name=Alex Ron.
http://127.0.0.1:8000/my_app/student/my_data/?id=10&name=Alex%20Ron
views.py
def my_data(request):
    id=request.GET.get('id','Not available')
    name=request.GET.get('name','Not available')
    return render(request,'my_data.html',{'id':id,'name':name})
Output is here
Student ID : 10
Student Name : Alex Ron

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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