14π
β
You want to try to submit a form on the button click. You can then import the functions you want to run from the script and call them in your view. You then redirect to the same page.
I hope this helps!
index.html
<form method="post">
{% csrf_token %}
<button type="submit" name="run_script">Run script</button>
</form>
views.py
if request.method == 'POST' and 'run_script' in request.POST:
# import function to run
from path_to_script import function_to_run
# call function
function_to_run()
# return user to required page
return HttpResponseRedirect(reverse(app_name:view_name))
π€James
4π
Adding to answer above. You can run the function in a different view completely:
<form method="post" action="{% url 'app:view/function' %}">
{% csrf_token %}
<button class="btn btn-danger btn-block btn-round">Perform task</button>
</form>
And render whatever template you want (same template will execute task but seem like nothing has happened). This is handy if you already have a βPOSTβ form handler.
π€Josh
- How to connect PyCharm to a Heroku postgres database
- Django (1.10) override AdminSite
- Django REST Framework different depth for POST/PUT?
- Django Haystack: filter query based on multiple items in a list.
- Django ForeignKey which does not require referential integrity?
Source:stackexchange.com