Leon
Leon

Reputation: 6554

Variable from post() to get_context_data() methods of TemplateView

On page bills.html I have form (action - order_page.html).

On page order_page.html I have form, one of that fields must have value from bills.html`s form.

views.py:

class OrderPage(TemplateView):
    template_name = 'order_page.html'
    form_class = BillAmount

    def post(self, request, *args, **kwargs):
        context = self.get_context_data()
        form = self.form_class(request.POST)

        if form.is_valid():
            amount = form.cleaned_data['sum'] # this value I need in get_context_data()

        return super(OrderPage, self).render_to_response(context)

    def get_context_data(self, **kwargs):

        amount = 2314 # here I need value from post()

        ctx = super(OrderPage, self).get_context_data(**kwargs)
        ctx['form'] = FinishPaymentForm(instance=payment) # another form
        return ctx

I have no idea, how to do this. I try in post(): OrderPage.amount = N and many other variants, but they does not work.

How to do this?

Thanks!

Upvotes: 1

Views: 2065

Answers (1)

mimo
mimo

Reputation: 2629

Try this:

class OrderPage(TemplateView):
    ...
    amount = None

    def post(self, request, *args, **kwargs):
        ...

        if form.is_valid():
            self.amount = form.cleaned_data['sum']
        ...

    def get_context_data(self, **kwargs):

        # Here you can use self.amount
        ...

Upvotes: 1

Related Questions