Book Image

Building Web Applications with Flask

By : Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia
Book Image

Building Web Applications with Flask

By: Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia

Overview of this book

Table of Contents (17 chapters)
Building Web Applications with Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Beyond GET


So far we've had a few cozy examples with Ajax and RESTful Web services but we have yet to record data in our database using a service. How about trying that now?

Recording to the database using Web services is not much different from what we have done in the previous chapter. We'll receive data from an Ajax request, we will check which HTTP method was used in order to decide what to do, then we'll validate the sent data and save everything if no error was found. In Chapter 4, Please Fill in This Form, Madam, we talked about CSRF protection and its importance. We'll keep validating our data against CSRF with our Web service. The trick is to add the CSRF token to the form data being submitted. See the attached code provided with the eBook for the example HTML.

This is how our view looks like with POST, PUT, and REMOVE method support:

@app.route("/articles/", methods=["GET", "POST"])
@app.route("/articles/<int:article_id>", methods=["GET", "PUT", "DELETE"])
def articles(article_id...