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.

<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

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

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.

<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.

# 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.

<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>
# 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.

post by Pravin


Comments