我正在尝试在POST方法中获取表单后更新表单字段。当我尝试更新表单时,我收到以下错误
Request Method: POST
Django Version: 1.6.6
Exception Type: TypeError
Exception Value:
'SmsPduForm' object does not support item assignment
我的表格代码是
class SmsPduForm(forms.Form):
"""
This class generates the SMS PDU form
"""
pduType = (('1','SMS-SUBMIT'), ('2','SMS-DELIVER'))
encoding = (('0','7 BIT'), ('8','UCS2'))
Address = forms.CharField(max_length=20, min_length=5, required=True, label="Target Address",
initial="+917654321")
SMSC = forms.CharField(max_length=13, min_length=5,required=True, label="SMSC Address",
initial="+91244414")
PDUType = forms.ChoiceField(widget=forms.RadioSelect, choices=pduType, required=True,
label="PDU Type", initial=pduType[0])
EncodingType = forms.ChoiceField(widget=forms.RadioSelect, choices=encoding, required=True, label="Encoding Type",
initial=encoding[0])
Text = forms.CharField(widget=forms.Textarea(attrs={'row':6, 'cols':50}),required=True, label="SMS Text",
initial="Hello", max_length=140)
smsPDU = forms.CharField(widget=forms.Textarea(attrs={'row':6, 'cols':50}),required=True, label="SMS PDU",
initial="Hello", max_length=140)
#Meta class of the form
class Meta:
fields = ('Address', 'SMSC', 'PDUType',
'EncodingType', 'Text', 'smsPDU')
我的观看代码是
def smsHandler(request):
context = RequestContext(request)
if request.method == 'POST':
method=""
#Do for post method else do for get method
smsPduForm = SmsPduForm(request.POST)
smsc = smsPduForm['SMSC'].value()
address = smsPduForm['Address'].value()
encodingType = smsPduForm['EncodingType'].value()
pduType = smsPduForm['PDUType'].value()
text = smsPduForm['Text'].value()
smsPdu = smsPduForm['smsPDU'].value()
print(smsc,address,encodingType,pduType,text,smsPdu)
if "Encode" in request.POST:
method = "encode"
smsEncObj = SmsEncoder(encodingType, smsc, pduType, text, address )
smsPdu = smsEncObj.generateSmsSubmitPdu()
#SmsPduForm.clean_smsPDU()
#Here I am updating the form
smsPduForm['smsPDU'] = smsPdu
elif "Decode" in request.POST:
method = "decode"
print("Action need to perform:",method)
contextDic = {'forms':SmsPduForm}
return render_to_response('telecom_utils/smsUtil.html',contextDic,context)
else:
contextDic = {'forms':SmsPduForm}
return render_to_response('telecom_utils/smsUtil.html',contextDic,context)
我如何更新表格?请仔细研究这个问题。
答案 0 :(得分:0)
您可以使用表单中字段的初始属性来更新值。 示例:
def smsHandler(request):
# --- your logic
#Here I am updating the form
sms_form = smsPduForm()
sms_form.fields["smsPDU"].initial = smsPdu
# --- next logic