Django. Only change in the database via html

Asked

Viewed 125 times

1

I’ve been developing in Django for a little while, I have an image like on my site, when I click on it I would like to only change a field in the bank without reloading or resending the page and stay in the same place.

I have how to call a Python function directly in html or via javascript which is the best way to do this?

I searched in several places and did not find an answer, the only way I found was passing via url, but then would have to render the page again?

I’m using Django 2.1 and python 3.7 Thanks in advance for the help.

  • Welcome to Stackoverflow. I believe the answer is Ajax. Read on: https://answall.com/questions/116153/ajax-n%C3%A3o-%C3%A9-a-program-language%C3%A7%C3%A3o-ent%C3%A3o-o-que-%C3%A9

  • Thanks Wallace, I’m really new in this world javascript and html, I’ve heard of Ajax, but I never stopped to look, I’ll study, thanks for the link. Hugs

  • I recently used the Axios and particularly it worked well with Django/Flask in a first test.

1 answer

1


When you want to send something to backend (your system) without having to reload the page, then you will need to use an AJAX call using javascript. In that post and in that you will see a way to do this. But it would be something more or less like this:

  <script>
    $("#image").onclick(function () {

      $.ajax({
        url: '/url/da/view/que/vai/processar/essa/chamada/',
        data: {
          'id': algumacoisa.id,
        },
        dataType: 'json',
        success: function (data) {
            alert("A user with this username already exists.");
        }
      });

    });
  </script>
  • Thanks Mazulo, I went to study some Jquery and Ajax and I was able to assemble with this example... Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.