How To Pass Variable From Template To Views In Django


While using django, you have multiple ways to pass data from template to views.py. You can pass data as URL qualifier or as URL queryset. You can use whichever method suits your requirement. ## 1. URL qualifier Here, you can pass data as URL qualifier //localhost:8000/country/state/city Here is an example of html page, please note the `href` parameter of link. ```html <a href="India/Maharashtra/Pune"> Pune</a><br> <a href="India/Maharashtra/Saswad"> Saswad</a><br> <a href="Aus/Queensland/Brisbane"> Brisbane</a><br> <a href="India/Madhya Pradesh/Indore"> Indore</a><br> <a href="search/?id=55&name=pravin"> Search</a><br> ``` You need to define url parameter to accept the required values `app1\urls.py` ```python from django.urls import path from . import views app_name = 'app1' urlpatterns = [ path('', views.index, name='index'), path('<country>/<state>/<city>/',views.detail, name='detail'), ] ``` In view, you need to define expected values at function definition level. `app1\views.py` ```Python def detail(request,country, state,city): context = "This is coming from view" print(" *** country", country) print(" *** state", state) print(" *** city", city) return render(request, 'app1/detail.html',{'context':context,'country':country,'state':state,'city':city}) ``` ## 2. Query Parameters Using GET Suppose you want to pass parameteras as query. e.g. http://localhost:8000/?id=123 This can be achieved by passing these values as query parameters. Here is sample form. Please note this is **GET** request form. ```html <form action="search" method="get"> <input name="id" type="text" placeholder="id..."> <input name="name" type="text" placeholder="name..."> <input name="age" type="text" placeholder="age..."> <button type="submit" class="btn btn-primary">Submit</button> </form> ``` You dont need any change in `urls.py` for tramsnitting values and you can reach the queryset value in `views.py` as below. ```python # version 1: # If a specific key is mandatory you can use: # This will return a value of a if the key exists and an exception if not. key_a = request.GET['a'] # Version 2: # If your keys are optional: key_a = request.GET.get('a') ``` You can try that without any argument and this will not crash. So you can wrap it with try: except: and return HttpResponseBadRequest() in example. This is a simple way to make your code less complex, without using special exceptions handling. ## 3. Query Parameters POST This is same as that of second option, only different being, this is post method. ```html <form action="find/" method="post"> {% csrf_token %} <input name="id" type="text" placeholder="id..."> <input name="name" type="text" placeholder="name..."> <input name="age" type="text" placeholder="age..."> <button type="submit" class="btn btn-primary">Submit</button> </form> ``` ```python # version 1: # If a specific key is mandatory you can use: # This will return a value of a if the key exists and an exception if not. key_a = request.POST['a'] # Version 2: # If your keys are optional: key_a = request.POST.get('a') ``` You can try that without any argument and this will not crash. So you can wrap it with try: except: and return HttpResponseBadRequest() in example. This is a simple way to make your code less complex, without using special exceptions handling.

This post is written by Pravin

Tags : Django

Share it :      

Leave a comment

captcha