Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Loading comments using AJAX


Now that we are able to create validated comments using AJAX, let's use the API to retrieve the list of comments and display them beneath the blog entry. To do this, we will read the values from the API and dynamically create DOM elements to display the comments. As you might recall from the earlier API responses we examined, there is quite a bit of private information being returned, including the entire serialized representation of each comment's associated Entry. For our purposes, this information is redundant and will furthermore waste bandwidth.

Let's begin by doing a bit of additional configuration to our comments endpoint to restrict the Comment fields we return. In api.py, make the following addition to the call to api.create_api():

api.create_api(
    Comment,
    include_columns=['id', 'name', 'url', 'body', 'created_timestamp'],
    methods=['GET', 'POST'],
    preprocessors={
        'POST': [post_preprocessor],
    })

Requesting the list of comments...