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"

Ajax in Django:-   


Ajax means Asynchronous JavaScript or Jquery and XML, it is used to partially update the content of the web page excluding complete web form.



Ajax improves the 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 the email already exists when the user switches to the next text field then we can create ajax code to check the email id.


when we select the country then the state list and when we select the state then the 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, which means if we select a dropdown list and display data into another dropdown list, it 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 the 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 the database and show into the 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