akonsu
akonsu

Reputation: 29566

map form fields to POST data

I need to consume a POST request from an external server, not from my application client. The request has this body:

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

For that I have created a form:

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

How do I tell Django that the form's to_country field should be populated from the ToCountry data item, etc?

Upvotes: 1

Views: 154

Answers (1)

karthikr
karthikr

Reputation: 99660

One way to do it would be:

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.  

Upvotes: 1

Related Questions