I am working on a django based project in which I have integrated ML 
trained models to check if a https url is legitimate or not. for this I 
need javascript or ajax to call a rest api for my form in which I want to 
send a post request so that I can check if a https url is legitimate or not.
*NOTE: My code is running successfully and giving correct answers on 
postman.* so just want to integrate it with my HTML form.
*My work:*
*form.html:*
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link 
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"; 
rel="stylesheet" 
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
 
crossorigin="anonymous">

    <title>SITE URL Form</title>
  </head>
  <body class="container">
      <form role="form" class="form" onsubmit="return false;">
      <div class="form-group">
        <label for="data">SITE URL</label>
        <textarea id="data" class="form-control" rows="5"></textarea>
      </div>
      <button id="post" type="button" class="btn btn-primary">POST</button>
    </form>

    <div id="output" class="container"></div>

    <script src="/axios.min.js"></script>
    <script>
      (function () {
        var output = document.getElementById('output');
        document.getElementById('post').onclick = function () {
          var data = document.getElementById('data').value;

          axios.post('http://127.0.0.1:8000/predict/', JSON.parse(data))
            .then(function (res) {
              output.className = 'container';
              output.innerHTML = res.data;
            })
            .catch(function (err) {
              output.className = 'container text-danger';
              output.innerHTML = err.message;
            });
        };
      })();
    </script>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script 
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js";
 
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
 
crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script 
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"; 
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
 
crossorigin="anonymous"></script>
    <script 
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"; 
integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
 
crossorigin="anonymous"></script>
    -->
  </body>
</html>

*myapp/urls.py:*
path('form/', form, name="form"),
    path('predict/', predict, name='predict')
*this is the response I am returning:*
if list(model.predict([test]))[0] == 1:
        return JsonResponse({"Response":"Legitimate"})
    else:
        return JsonResponse({"Response":"Phishing or fake"})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e81cf4f0-1208-4623-97f9-4b88f01d67b3n%40googlegroups.com.

Reply via email to