将表单从一个请求传递到另一个请求
Passing Forms from one request to another
我想实现这个场景:我想要一个简单的表单,包含一些文本字段和多个提交按钮。当我单击一个按钮时,应该执行此按钮的操作并且应该更新相应的表单字段。比方说,我们有一个加密演示。当我按下加密时,明文被读取并加密写入密文字段等。
我有一个非常简单的表格:
from django import forms
class ABEForm(forms.Form):
master_private_key = forms.CharField(required=False,label="Master Secret Key",widget=forms.Textarea(attrs={'width':"100%", 'cols' : "80", 'rows': "20", }))
master_public_key = forms.CharField(required=False,label="Master Public Key",widget=forms.Textarea(attrs={'width':"100%", 'cols' : "80", 'rows': "20", }))
还有一个非常简单的观点:
def form_submit(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
#check what submit button was pressed
if 'abe_setup' in request.POST:
form = ABEForm(request.POST)
abesetupdata = ABE_Scheme_SetupData()
abesetupdata.init_setup()
form.master_private_key = abesetupdata.master_key
form.master_public_key = abesetupdata.master_public_key
print("privkey =>", form.master_private_key)
print("pubkey =>", form.master_public_key)
return render(request, 'webfrontend/abeform.html', {'form': form})
elif 'abe_keyen' in request.POST:
return render(request, 'webfrontend/abeform.html', {'form': form})
# if a GET (or any other method) we'll create a blank form
else:
form = ABEForm()
return render(request, 'webfrontend/abeform.html', {'form': form})
案件处理(按下哪个按钮)似乎没问题。但是,每当我按下设置按钮并且重新呈现表单时,表单都是空的。我错过了什么?我知道您可以提供字典来将数据绑定到表单。但是使用 request.POST
作为数据源应该可行,对吗?
打印语句确实有效并将表单内容打印到命令行...
我认为当未绑定且无法分配字段数据时,表单是不可变的。我必须创建新表单,像这样在其中传递字典:
data = {'master_private_key': abesetupdata.master_private_key,
'master_public_key': abesetupdata.master_public_key,
'attribute_key' : abesetupdata.attribute_key,
}
form = ABEForm(data)
我想实现这个场景:我想要一个简单的表单,包含一些文本字段和多个提交按钮。当我单击一个按钮时,应该执行此按钮的操作并且应该更新相应的表单字段。比方说,我们有一个加密演示。当我按下加密时,明文被读取并加密写入密文字段等。
我有一个非常简单的表格:
from django import forms
class ABEForm(forms.Form):
master_private_key = forms.CharField(required=False,label="Master Secret Key",widget=forms.Textarea(attrs={'width':"100%", 'cols' : "80", 'rows': "20", }))
master_public_key = forms.CharField(required=False,label="Master Public Key",widget=forms.Textarea(attrs={'width':"100%", 'cols' : "80", 'rows': "20", }))
还有一个非常简单的观点:
def form_submit(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
#check what submit button was pressed
if 'abe_setup' in request.POST:
form = ABEForm(request.POST)
abesetupdata = ABE_Scheme_SetupData()
abesetupdata.init_setup()
form.master_private_key = abesetupdata.master_key
form.master_public_key = abesetupdata.master_public_key
print("privkey =>", form.master_private_key)
print("pubkey =>", form.master_public_key)
return render(request, 'webfrontend/abeform.html', {'form': form})
elif 'abe_keyen' in request.POST:
return render(request, 'webfrontend/abeform.html', {'form': form})
# if a GET (or any other method) we'll create a blank form
else:
form = ABEForm()
return render(request, 'webfrontend/abeform.html', {'form': form})
案件处理(按下哪个按钮)似乎没问题。但是,每当我按下设置按钮并且重新呈现表单时,表单都是空的。我错过了什么?我知道您可以提供字典来将数据绑定到表单。但是使用 request.POST
作为数据源应该可行,对吗?
打印语句确实有效并将表单内容打印到命令行...
我认为当未绑定且无法分配字段数据时,表单是不可变的。我必须创建新表单,像这样在其中传递字典:
data = {'master_private_key': abesetupdata.master_private_key,
'master_public_key': abesetupdata.master_public_key,
'attribute_key' : abesetupdata.attribute_key,
}
form = ABEForm(data)