jundymek
jundymek

Reputation: 1043

Run python function using custom button in Django admin

I added custom button into my app_index.html file:

<form action="{% url 'admin:index' %}">
    <input type="submit" value="{% trans 'Another Button' %}" 
name="_anotherbutton" />
</form>

I have simple function:

def printer():
    print('Some text')

How can I run this function using my custom button? Now it maps to admin:index but I would like it to run printer function and go to another view (or show alert message using jquery - I will think about it). Printer function is just an example - in real it will do some database search/modify database and go to result view.

Upvotes: 2

Views: 749

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

You need to stop thinking in terms of functions. You need to think in terms of views and URLs. The only way your frontend can communicate with your backend is via a view called from a URL. So, you need to call your function from a view, and associate a URL with it, which your button calls.

Upvotes: 1

Related Questions