[Answered ]-HTML form action with django and custom script

2👍

If you POST to salesforce instead of your own app then you won’t be able to make use of all that form error displaying or the validation that happens when your view checks the post data. The user will goto sales force and then be redirected to retURL (I’m guessing).

If you want to use all that django form stuff and still be able to send the data to salesforce you could send the post from your Django view after you’ve confirmed the form is valid.

You would use the data from your valid form to build a POST request to salesforce. This example uses Requests, but you could use whichever urllib you’d like.

import requests

#do some testing to see that this code is correct
SALES_FORCE_SUCCESS_CODE = 201

def form_view(request):
    sales_force_params = {'oid': 'example_id' , 'retURL':'http://example.com'} 
    #You may not need to include the retURL
    if form.is_valid():
        post_params = form.cleaned_data
        post_params.update(sales_force_params)
        response = requests.post("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST", params=post_params)

        if response.status_code != SALES_FORCE_SUCCESS_CODE:
            pass 
            #Either your validation wasn't strict enough or something else happened

Leave a comment