X
X
Xmahopnya2020-01-21 11:06:35
Django
Xmahopnya, 2020-01-21 11:06:35

How to execute Django function on button click in html template?

There is a function implemented in the django view, it returns some value. How to call a function from the frontend? I can assume that this is done through django forms and post requests, but I could be wrong. I have no idea how to do this if there are several such functions, but all types are implemented in one view

if request == 'POST':
    код

For example, in php this is done by simply specifying the button id:
<?php

function testfun()
{
echo "Your test function on button click is working";
}

if(array_key_exists('test',$_POST)){
testfun();
}

?>

I ran through the documentation, I didn’t find anything particularly similar (maybe I was looking badly). Put me on the right path, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Larisa .•º, 2020-01-21
@Xmahopnya

as an option

@register.simple_tag
def your_custom_tag_django (url):
    return "window.location='" + url +"';

<form action="/add/book" method="post">{% csrf_token %}
    {{ form }}
    <button onclick="{% your_custom_tag_django '/books' %}">Cancel</button>
    <button type="submit">OK</button>
</form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question