从Template()的post()到get_context_data()方法的变量

时间:2014-11-30 09:46:18

标签: python django

在页面 bills.html 我有表单(操作 - order_page.html )。

在页面 order_page.html 我有表单,其中一个字段必须具有来自 bills.html 的表单的值。

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

我不知道,怎么做。我尝试使用 post()OrderPage.amount = N和许多其他变体,但它们不起作用。

怎么做?

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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
        ...
相关问题