Answer the question
In order to leave comments, you need to log in
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':
код
<?php
function testfun()
{
echo "Your test function on button click is working";
}
if(array_key_exists('test',$_POST)){
testfun();
}
?>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question