简体   繁体   中英

How make a call to a function in views.py file in Django from ajax-html in template folder?

I am working with Django. I want to make a call to a function in views.py of an apps from html file which is template/app_name/login.html . This function is called for login validation. my code snippet: index.html :

<script >
  function subLogin()
  {
    var emailid = document.getElementById("emailid").value;
    var password = document.getElementById("password").value;
    if(emailid != "" && password != "")
    {
      if(emailid.length >= 4)
      {
        if(password.length >= 8)
        {
          $.ajax({
            url: '/test/',
            data: {'emailid': emailid, 'password': password},
            type: 'POST',
            dataType: 'json',
            success: function(json) {
              //alert(json['success']);
              if(json['success'] == false)
              {
                if(json['email'] == false)
                  alert('The Email ID Provided is not correct!');
                else
                  alert('The Email ID & Password Provided Do Not Match!');
              }

def test(request):
logger.info("inside check password function....")
if (request.is_ajax()):
    emailid = request.POST['emailid']
    password = request.POST['password']
else:
    emailid = ''
    context = {'emailid':emailid}
return render(request, 'index.html', context)

This is my test function sitting in myapp/views.py file. But unable to make this call from ajax.

I think I need to rectify the call from ajax code.

Suppose my views.py has " def test(request) :" function. So,I want to call this function from html file to this test() with email and password as post parameters.

Any help is highly appreciated.

Ajax requests are just like any other request. That is, you need a URL pointing to a view that returns an HttpResponse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM