blob: 7a1b07fc745503edc0466206de40f4bc5f24bf8d (
plain)
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
37
38
39
|
from flask import Flask, render_template, request, redirect, url_for
from peewee import SqliteDatabase
from models import Post
from forms import PostForm
app = Flask(__name__)
app.config.from_object('config')
db = SqliteDatabase('blog.db')
@app.before_request
def before_request():
db.connect()
@app.after_request
def after_request(response):
db.close()
return response
@app.route('/')
def index():
posts = Post.select().order_by(Post.created_at.desc())
return render_template('index.html', posts=posts)
@app.route('/post/<int:post_id>')
def post(post_id):
post = Post.get(Post.id == post_id)
return render_template('post.html', post=post)
@app.route('/create', methods=['GET', 'POST'])
def create():
form = PostForm()
if form.validate_on_submit():
Post.create(title=form.title.data, content=form.content.data)
return redirect(url_for('index'))
return render_template('create.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
|