Python - 301 - Flask
  • 00 - Appealing API's
  • 01- A Smooth Start
  • 02 - Chief Configuration
  • 03 - Example Endpoint and an Adornable App
  • 04 - Modeling the Models
  • 05 - Scheming Schemas
  • 06 - Making Migrations
  • 07 - Uniformed Users
  • 08 - Alarming Authentication
  • 09 - Account Actions
  • 10 - Postman Prevalence and Examining Endpoints
  • 11 - Blog Post Blogging
Powered by GitBook
On this page
  • The Blog Post Views
  • Create Blog Post
  • Delete Post
  • Get All
  • Get one Blog Post
  • Update Blog Post
  • Custom Response
  • Registering the Blue Print
  • Final Steps

Was this helpful?

11 - Blog Post Blogging

In this module, we'll create the views needed to finish the API by implementing actions to use on the Blog Post model

Previous10 - Postman Prevalence and Examining Endpoints

Last updated 6 years ago

Was this helpful?

The Blog Post Views

Now that we have all of the actions needed for the users, we still need to present, create, and manipulate the blog post model. Just like how we created the user_view file that contained all the user actions, we're going to use blog_post_views.py file to handle the blog post traffic. Then, we will add the blueprint to the app module, so we have access to those routes

Create Blog Post

Start off by adding the following code to views/blog_post_view.py . This will start off by creating the blueprint and importing all of the dependencies needed for the views

Delete Post

We don't want a random user to be able to delete someone else's post. So we'll need to add that functionality. Luckily, Flask comes with the g object we've discussed earlier. g holds everything Flask needs and anything else we need to persist, into a dictionary. Add the following function to views/blog_post_view.py

Get All

This method is really short. All it does is grab all of the posts and returns them. There isn't a lot of room for error, so we don't need to handle anything that can go wrong. Either something gets returned, or nothing does

Get one Blog Post

Instead of grabbing all of the posts, what if we just want to grab one? well, if we remember, we added a method already in the BlogPost model to do this. We just need to call, save the object, and return it

Update Blog Post

Custom Response

We've seen this before with the user object but we never implemented it, nor imported it here. Let's do that

Registering the Blue Print

Make the create_app function in src/app.py file look like the following

Final Steps

Run the API using the steps we took in module 10, and test the endpoints with postman just like we did before