Ajax in Django:-   Ajax means Asynchronous JavaScript and XML, it is used to partially update the content of web page excluding complete web form.



Ajax improves performance of web application and provide dynamic programming approach without reloading and submit the data.





for example if we create a registration form and we want to check that email already exists when user switch to next text field then we can create ajax code to check email id.





when we select country then state list and when we select the state then city list will be populated by AJAX.





when we search from facebook or google search box then data will be populated when we press any key that will be managed by Ajax.









Role of JavaScript or Jquery:-



It is used to handle the event in Ajax Process, means if we select dropdown list and display data into another dropdown list will be managed by AJAX.





Role of XML:-

XML means Extensible markup language which is used to transfer data from client machine(Browser) to server machine (Apache Server) and server machine to server machine.



XMLHttpRequest:-  this class is used to send data from browser to web server.



XMLHttpResponse:-  It is used to get web server response using XML form.







Create Search Box which will search respective record from database and show into browser?





1) Create Two Different Method to load and action using views.py and define into urls.py





path('ajaxload',views.ajaxload,name='ajaxload'),


path('ajaxdata',views.ajaxdata,name='ajaxdata'),

def ajaxload(request):
    return render(request,"scsapp/ajaxsearch.html")
def ajaxdata(request):
    data = request.GET["q"]
    result = Register.objects.filter(fullname__contains=data)
    return render(request,"scsapp/ajaxdata.html",{'res':result})


2)  Create html page under views folder ajaxsearch.html and ajaxdata.html


    Write code ajaxsearch.html of AJAX using JS

    <!DOCTYPE html>
<html>
<head>
<title></title>
    <script type="text/javascript">
    function showdata(a)
    {
             xmlhttp = new XMLHttpRequest();
             xmlhttp.onreadystatechange=function()
             {
              document.getElementById("res").innerHTML=xmlhttp.responseText; 

             }
             xmlhttp.open("get","ajaxdata?q="+a,true);
             xmlhttp.send();


    }

    </script>
</head>
<body>
   Type and char to search user's<br>
   <input type="text" id="txtseaarch" onkeyup="showdata(this.value)">
   <div id="res"></div>
</body>
</html>

3)  ajaxdata.html

<table border="1">
<tr><th>EmailID</th><th>Password</th><th>Fullname</th></tr>
 {% for q in res %}
     <tr>  <td>{{ q.emailid }} </td><td> {{ q.password }} </td><td> {{ q.fullname }}</td></tr>
 {% endfor %}

</table>



1 Comments

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

  1. Sir,

    def ajaxdata(request):
    data = request.GET["q"]
    result = Register.objects.filter(fullname__contains=data)
    return render(request,"scsapp/ajaxdata.html",{'res':result})


    What is Register Here??

    ReplyDelete

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