首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用POST方法后,Django 1.6.6中的表单字段更新值

使用POST方法后,Django 1.6.6中的表单字段更新值
EN

Stack Overflow用户
提问于 2015-01-06 04:01:18
回答 1查看 122关注 0票数 0

在POST方法中获取表单后,我正在尝试更新表单字段。当我试图更新表单时,我得到了以下错误

代码语言:javascript
复制
Request Method: POST
Django Version: 1.6.6
Exception Type: TypeError
Exception Value:    
'SmsPduForm' object does not support item assignment

我的表单代码是

代码语言:javascript
复制
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')

我的视图代码是

代码语言:javascript
复制
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)

如何更新表单?请调查一下这个问题。

EN

回答 1

Stack Overflow用户

发布于 2015-01-06 04:16:58

您可以在表单中使用字段的初始属性来更新值。示例:

代码语言:javascript
复制
def smsHandler(request):
    # --- your logic        
            #Here I am updating the form
            sms_form = smsPduForm()
            sms_form.fields["smsPDU"].initial = smsPdu
    # --- next logic 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27786946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档