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 get the value of the selected drop-down list in Django after form submission:-


If we selected the drop-down list value in Django and submit the form then this value will be reset.

if we want to display the selected value on dropdownlist then we use the following code.


1)  Code for views.py

def search(request):

   if request.method=="POST":

      result = Register.objects.filter(fullname=request.POST['fname'])

      res = Register.objects.all()

      return render(request,"frontdesk/search.html",{'data':res,'data1':result,'fd':request.POST['fname']})

   res = Register.objects.all()

   return render(request,"frontdesk/search.html",{'data':res})   


2)  code for HTML web page for designing


<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>


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

    {% csrf_token %}

     <select name="fname">

      <option value="Select name">Select name</option>

         {%  for d in data %}

          {%  if d.fullname == fd %}

         <option value="{{d.fullname}}" selected="selected">{{d.fullname}}</option>

         {% else %}

         <option value="{{d.fullname}}" >{{d.fullname}}</option>

           {% endif %} 

         {% endfor %}


          </select>


     <br><br>

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

</form>


    <table><tr><th>Email</th><th>Fullname</th><th>Mobile</th></tr>

{%  for  d1 in data1  %}

     

       <tr><td>{{d1.emailid}}</td><td>{{d1.fullname}}</td><td>{{d1.mobile}}</td></tr> 


{%  endfor  %}

   </table>


</body>

</html>





Another approach to display selected element on the dropdownlist:-


<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>


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

    {% csrf_token %}

     <select name="fname">

      <option value="Select name">Select name</option>

         {%  for d in data %}

          

         <option value="{{d.fullname}}" {%  if d.fullname == fd %} selected="selected"  {% endif %} >{{d.fullname}}</option>

        

          

         {% endfor %}


          </select>


     <br><br>

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

</form>


    <table><tr><th>Email</th><th>Fullname</th><th>Mobile</th></tr>

{%  for  d1 in data1  %}

     

       <tr><td>{{d1.emailid}}</td><td>{{d1.fullname}}</td><td>{{d1.mobile}}</td></tr> 


{%  endfor  %}

   </table>


</body>

</html>



How to check Query set is empty or not in Django?


code of views.py under Django:-

res = Register.objects.all()

return render(request,"frontdesk/search.html",{''data1':res)


code of  Template file to check Query set value is null or not null?


 {% if data1 %}

    <table><tr><th>Email</th><th>Fullname</th><th>Mobile</th></tr>

{%  for  d1 in data1  %}

     

       <tr><td>{{d1.emailid}}</td><td>{{d1.fullname}}</td><td>{{d1.mobile}}</td></tr> 


{%  endfor  %}

{%  endif %}

   </table>





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