Part 5 | Process Order
Overview:
It's time to complete the last part of this module and send off our form data for proccess orders.
We will first build out a view in the backend that will take our form data, then use the fetch API to send POST data from our submitFormData function.
Once we have our base view and POST request built in, we will finish this part with adding logic to our view to process orders.
Step 1 | Process Order View/url
1:01:17Step 2 | Send POST Data
1:03:14Now we can create a post call and send the data to the backend. Remember, we still have access to the csrf token because we inherit from main.html
Inside our "submitFormData" function (checkout.html), let's create a post request using "fetch()".
Set the variable of "url" to the new path we just created in the last step. Next, add in the csrf token along with a Javascript object while nesting our user information and shipping data.
Remember to stringify the data.
Once we send the POST data, let's send the user back to our home page in the promise.
View Source CodeStep 3 | Transaction ID
1:07:51Step 4 | Set Data
1:08:56Now we want to parse the data sent from the post request and query/create some data if a user is authenticated. Again, we will ignore unauthenticated users until the next module.
We will use json.loads() to parse the data. Once we set the return value to the "data" variable, we can query the items inside as a Python dictionary.
View Source CodeStep 5 | Confirm Total
1:11:33Step 6 | Shipping Logic
1:13:06Last, we need to create an instance of the shipping address if an address was sent.
We can add this just underneath our total confirmation. Check the source code link if you are unsure exactly where in the view we are adding this.
View Source Code