1

This is some content of my Django template.

...
<a id="ID" href="">Do operation with database.<a/>
...
<script type="text/javascript">
                    window.onload = function () {
                        var a = document.getElementById("ID");
                        a.onclick = function () {
                            if (confirm("do you really want to perform task?")) {
                                /** call python function in order to perform some operations with database **/
                            }
                            return;
                        }
</script>
...

The function is for example(we can imagine the function is in views.py):
def performtask():
#do my operation with the database

My question is, how it is possibile to call performtask() function from the script tag in my template?

1
  • 1
    Nah, the javascript is executed client-side. You would have to send a request (e.g. AJAX) and handle that request in a view in order to execute code on the server. Commented Sep 26, 2017 at 12:00

1 Answer 1

1

you should go through with ajax

   $.ajax({
        url: "http://abc,com/app/",
        type: "POST",
        data :{},
        success:function(result){
            //your code
        },
        error:function(error){
            //error handal
        }
    });

you can get more details here :

https://realpython.com/blog/python/django-and-ajax-form-submissions/

https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

Sign up to request clarification or add additional context in comments.

2 Comments

What is the exact content of url line? I can't understand it
whaterver url you defined in urls.py file for corresponding views(method). like ( url: "/app/", url: "/accounts/")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.