将表单字段映射到POST数据

时间:2014-06-19 01:18:37

标签: django django-forms

我需要从外部服务器使用POST请求,而不是从我的应用程序客户端。请求有这个机构:

ToCountry=US&ToState=CA&SmsMessageSid=SM4047b31943de6a4e6f78c9328a069daf...

为此,我创建了一个表单:

class MyForm(forms.Form):
    to_country = forms.CharField()
    to_state = forms.CharField()
    sms_message_sid = forms.CharField()
    ...

如何告诉Django应该从to_country数据项等填充表单的ToCountry字段?

1 个答案:

答案 0 :(得分:1)

一种方法是:

if request.method == "POST":

    initial = { 
                     'to_country' : request.POST.get('ToCountry'),
                       'to_state' : request.POST.get('ToState'),
                'sms_message_sid' : request.POST.get('SmsMessageSid'),
                #rest of the fields
              }
    form = MyForm(initial=initial)
    #more code here.