For starters, validate the form before getting its data
smsPduForm = SmsPduForm(request.POST)
if smsPduForm.is_valid():
d = smsPduForm.cleaned_data
smsc = d['SMSC']
# etc...
regarding the form assignment, I'm not too sure what you're trying to do.
¿Do you want to display the same form with the same data but changing a
field? In that case I think you need to instantiate a new Form object with
the data from the old one, changing the corresponding field.
form_data = smsPduForm.data
form_data['smsPDU'] = smsPdu
new_smsPduForm = SmsPduForm(form_data)
either way, since the first request was made via POST then you should
redirect to a different page instead of rendering a new one in the same
request cycle, otherwise if the user refreshes the new page then your logic
will be executed twice.
On Mon, Jan 5, 2015 at 5:23 PM, rajat shukla <[email protected]>
wrote:
> I am trying to update a form field after getting a form in the POST
> method. When I am trying to update the form I am getting following error
>
> Request Method: POST
> Django Version: 1.6.6
> Exception Type: TypeError
> Exception Value:
> 'SmsPduForm' object does not support item assignment
>
> My Form Code is
>
> 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')
>
> My view code is
>
> 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)
>
> How can i update the form ? Kindly look into the issue.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/75f83eb2-fa29-4022-9c9c-760f0060c209%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/75f83eb2-fa29-4022-9c9c-760f0060c209%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CALn3ei2%2BZEHe_uqxveDUSAZfvdg7z7nfajS3nxLhWCfgMsSYDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.