Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 How to bind dictionary, list, set and tuple object into Html form elements in Django:-

We will pass data from the view method as a LIST, Dictionary, or SET object and bind data under the dropdown list, checkbox, Listbox and radiobutton.

We will write python code under HTML file using Jinja Tag.

Code for

from django.shortcuts import render

def index(request):

course = ['C','CPP','JAVA','PYTHON','PHP']

branch = ['VijayNagar','PALASIA','BHAWARKUA']

student = {'101':'Adarsh','102':'Parag','103':'Sunil','104':'Firoz'}

feed = {45000,12000,23000,11000,89000}

if request.method=="POST":

course1= request.POST["course"]  #value from ddl

chkitem = request.POST.getlist("chk[]")  #value from checkbox


for data in chkitem:

chkitem1 = chkitem1+data + " "

si= request.POST.getlist("sinfo[]")  #value from listbox

s = ''

for data1 in si:

s=s+data1 + " "

fee = request.POST["r"]

output = "DDL " + course1 +  ", CHECKBOX " + chkitem1 + ", LISTBOX DATA " +s  + " ,RADIO BUTTON DATA "+fee

return render(request,"databinding/index.html",{'res':course,'res1':branch,'res2':student.items(),'res3':feed,'out':output})

return render(request,"databinding/index.html",{'res':course,'res1':branch,'res2':student.items(),'res3':feed})

Code for index.html

<!DOCTYPE html>

    <form action="" method="post">

     {% csrf_token %}

     <select name="course">
      {%  for  data in res  %}

        <option value="{{data}}">{{data}}</option>

      {% endfor %}


      {%  for  data in res1  %}

        <input type="checkbox" value="{{data}}" name="chk[]" />{{data}} 

      {% endfor %}

     <select name='sinfo[]' multiple="true">
      {%  for key,value in res2 %}
      <option value="{{key}}">{{value}}</option>
      {%  endfor %}
      {%  for  data in res3  %}

        <input type="checkbox" value="{{data}}" name="chk[]" />{{data}} 

      {% endfor %}

     <input type="submit" name="btnsubmit" value="Click" />


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post