B
B
Boldy2014-11-21 00:31:05
Django
Boldy, 2014-11-21 00:31:05

How to create a function call button in a django template?

There is a model:

class TreeNode(MPTTModel):
    account = models.OneToOneField(User, unique=True, verbose_name='Аккаунт', related_name='treenode')
    def request_cash_in(self):
        if self.available_for_cash_in > 0:
            from payments.models import CashingInRequest
            CashingInRequest.objects.create(total=self.available_for_cash_in, client=self, date=date.today())

How can I create a button in a template that, when clicked, calls the "request_cash_in" method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Scherbakov, 2014-11-21
@Boldy

Well, how can you imagine it? There are two methods.
Synchronous. Create a separate view that calls the mentioned function and point that button to its URL. After calling the function, it can redirect the user to the right place.
AJAX - no page reload. Again, you are creating a separate view that is called by a JavaScript function that is called when the button is clicked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question