-
Notifications
You must be signed in to change notification settings - Fork 0
/
cre_blog.py
36 lines (30 loc) · 1.05 KB
/
cre_blog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
db = SQLAlchemy(app)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
content = db.Column(db.Text)
@app.route('/')
def index():
posts = Post.query.all()
return render_template('index.html', posts=posts)
@app.route('/post/<int:id>')
def post_detail(id):
post = Post.query.get_or_404(id)
return render_template('post_detail.html', post=post)
@app.route('/post/new', methods=['GET', 'POST'])
def new_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
post = Post(title=title, content=content)
db.session.add(post)
db.session.commit()
return redirect(url_for('index'))
else:
return render_template('new_post.html')
if __name__ == '__main__':
app.run()