├── .gitignore ├── app.py ├── hello.py ├── init_db.py ├── schema.sql ├── static └── css │ └── style.css └── templates ├── base.html ├── create.html ├── edit.html ├── index.html └── post.html /.gitignore: -------------------------------------------------------------------------------- 1 | env 2 | __pycache__/ 3 | database.db 4 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | from flask import Flask, render_template, request, url_for, flash, redirect 3 | from werkzeug.exceptions import abort 4 | 5 | 6 | def get_db_connection(): 7 | conn = sqlite3.connect('database.db') 8 | conn.row_factory = sqlite3.Row 9 | return conn 10 | 11 | 12 | def get_post(post_id): 13 | conn = get_db_connection() 14 | post = conn.execute('SELECT * FROM posts WHERE id = ?', 15 | (post_id,)).fetchone() 16 | conn.close() 17 | if post is None: 18 | abort(404) 19 | return post 20 | 21 | 22 | app = Flask(__name__) 23 | app.config['SECRET_KEY'] = 'your secret key' 24 | 25 | 26 | @app.route('/') 27 | def index(): 28 | conn = get_db_connection() 29 | posts = conn.execute('SELECT * FROM posts').fetchall() 30 | conn.close() 31 | return render_template('index.html', posts=posts) 32 | 33 | 34 | @app.route('/') 35 | def post(post_id): 36 | post = get_post(post_id) 37 | return render_template('post.html', post=post) 38 | 39 | 40 | @app.route('/create', methods=('GET', 'POST')) 41 | def create(): 42 | if request.method == 'POST': 43 | title = request.form['title'] 44 | content = request.form['content'] 45 | 46 | if not title: 47 | flash('Title is required!') 48 | else: 49 | conn = get_db_connection() 50 | conn.execute('INSERT INTO posts (title, content) VALUES (?, ?)', 51 | (title, content)) 52 | conn.commit() 53 | conn.close() 54 | return redirect(url_for('index')) 55 | 56 | return render_template('create.html') 57 | 58 | 59 | @app.route('//edit', methods=('GET', 'POST')) 60 | def edit(id): 61 | post = get_post(id) 62 | 63 | if request.method == 'POST': 64 | title = request.form['title'] 65 | content = request.form['content'] 66 | 67 | if not title: 68 | flash('Title is required!') 69 | else: 70 | conn = get_db_connection() 71 | conn.execute('UPDATE posts SET title = ?, content = ?' 72 | ' WHERE id = ?', 73 | (title, content, id)) 74 | conn.commit() 75 | conn.close() 76 | return redirect(url_for('index')) 77 | 78 | return render_template('edit.html', post=post) 79 | 80 | 81 | @app.route('//delete', methods=('POST',)) 82 | def delete(id): 83 | post = get_post(id) 84 | conn = get_db_connection() 85 | conn.execute('DELETE FROM posts WHERE id = ?', (id,)) 86 | conn.commit() 87 | conn.close() 88 | flash('"{}" was successfully deleted!'.format(post['title'])) 89 | return redirect(url_for('index')) 90 | -------------------------------------------------------------------------------- /hello.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, escape 2 | 3 | app = Flask(__name__) 4 | 5 | 6 | @app.route('/') 7 | def hello(): 8 | return 'Hello, World!' 9 | 10 | 11 | @app.route('/greet') 12 | def greet(): 13 | name = request.args['name'] 14 | return ''' 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |

Hi {}

23 | 24 | '''.format(escape(name)) 25 | -------------------------------------------------------------------------------- /init_db.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | connection = sqlite3.connect('database.db') 4 | 5 | 6 | with open('schema.sql') as f: 7 | connection.executescript(f.read()) 8 | 9 | cur = connection.cursor() 10 | 11 | cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)", 12 | ('First Post', 'Content for the first post') 13 | ) 14 | 15 | cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)", 16 | ('Second Post', 'Content for the second post') 17 | ) 18 | 19 | connection.commit() 20 | connection.close() 21 | -------------------------------------------------------------------------------- /schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS posts; 2 | 3 | CREATE TABLE posts ( 4 | id INTEGER PRIMARY KEY AUTOINCREMENT, 5 | created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 6 | title TEXT NOT NULL, 7 | content TEXT NOT NULL 8 | ); 9 | -------------------------------------------------------------------------------- /static/css/style.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | border: 2px #eee solid; 3 | color: brown; 4 | text-align: center; 5 | padding: 10px; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | {% block title %} {% endblock %} 12 | 13 | 14 | 30 |
31 | {% for message in get_flashed_messages() %} 32 |
{{ message }}
33 | {% endfor %} 34 | {% block content %} {% endblock %} 35 |
36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /templates/create.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |

{% block title %} Create a New Post {% endblock %}

5 | 6 |
7 |
8 | 9 | 12 |
13 | 14 |
15 | 16 | 18 |
19 |
20 | 21 |
22 |
23 | {% endblock %} 24 | -------------------------------------------------------------------------------- /templates/edit.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |

{% block title %} Edit "{{ post['title'] }}" {% endblock %}

5 | 6 |
7 |
8 | 9 | 12 | 13 |
14 | 15 |
16 | 17 | 19 |
20 |
21 | 22 |
23 |
24 |
25 |
26 | 29 |
30 | {% endblock %} 31 | 32 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |

{% block title %} Welcome to FlaskBlog {% endblock %}

5 | {% for post in posts %} 6 | 7 |

{{ post['title'] }}

8 |
9 | {{ post['created'] }} 10 | 11 | Edit 12 | 13 |
14 | {% endfor %} 15 | {% endblock %} 16 | -------------------------------------------------------------------------------- /templates/post.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |

{% block title %} {{ post['title'] }} {% endblock %}

5 | {{ post['created'] }} 6 |

{{ post['content'] }}

7 | {% endblock %} 8 | --------------------------------------------------------------------------------