Loading

Quipoin Menu

Learn • Practice • Grow

flask / Blog Project
tutorial

Blog Project

Now you will build a simple blog application where users can create, edit, and delete posts. This project combines everything you have learned: templates, forms, and database.

Step 1: Create Post Model

class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
date_posted = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

Step 2: Create Post Form (Flask‑WTF)

class PostForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
content = TextAreaField('Content', validators=[DataRequired()])
submit = SubmitField('Post')

Step 3: Route to Create a Post

@app.route('/post/new', methods=['GET', 'POST'])
def new_post():
form = PostForm()
if form.validate_on_submit():
post = Post(title=form.title.data, content=form.content.data, author=current_user)
db.session.add(post)
db.session.commit()
flash('Your post has been created!', 'success')
return redirect(url_for('home'))
return render_template('create_post.html', form=form)

Step 4: Display Posts on Home Page

@app.route('/')
def home():
posts = Post.query.order_by(Post.date_posted.desc()).all()
return render_template('home.html', posts=posts)

Step 5: Edit and Delete Routes (Similar)

You will implement edit and delete using the same form and `db.session.delete()`.


Two Minute Drill
  • Create a `Post` model with title, content, date, and user foreign key.
  • Use a Flask‑WTF form to submit posts.
  • Save posts to the database and display them ordered by date.
  • Add edit and delete functionality for completeness.

Need more clarification?

Drop us an email at career@quipoinfotech.com