How to create simply posts website in Flask framework?

Zrzut ekranu 2015-06-28 o 00.26.17

I have used Django framework so far, but because of my participation in Google summer of code. We decided to use something new. It was Flask. And Now I want to share it with you.

It is a microframework, which allows us in really short time create a nice app. I’d like to share my experience in this area to teach you how you can create an app. You will have a possibility to add, delete and update posts.

First of all you need to install flask framework via pip in your command line:

pip install flask

Next step it will be prepare a files structures
Zrzut ekranu 2015-06-28 o 00.44.54

My structure of files looks:

  • forms( will contain all form objects in our case only post form)
  • templates will contain all app views(listing posts and possibility to create post)

We want to manage posts, so we have to define database model Post in I only defined title and text fields. Zrzut ekranu 2015-06-28 o 00.49.04

Zrzut ekranu 2015-06-28 o 01.11.58

Next step will be create a routes. The main purpose of routes is to recognize urls and execute actions. We need four methods to display list of posts(index), delete post(delete_post), add post(new_post) and update post(edit_post)

That’s all to run your first posts application. Additionally I attach link to project on github, where you can trace whole project code.

Have a nice coding!