gpt4 book ai didi

python - TemplateView 的 post() 到 get_context_data() 方法的变量

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:56 24 4
gpt4 key购买 nike

在页面 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 和许多其他变体,但它们不起作用。

如何做到这一点?

谢谢!

最佳答案

试试这个:

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
...

关于python - TemplateView 的 post() 到 get_context_data() 方法的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com