WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.

0 downloads

dbbasic-content/examples/flask_integration.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-content.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-content.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-content.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Release v0.2.0 - Pages support 25b7e5e 1 year, 1 month ago History
flask_integration.py · 1.4 KB · 60 lines
1"""
2Flask integration example for dbbasic-content.
3
4Shows how to build a simple blog with Flask.
5"""
6
7from flask import Flask, render_template, abort
8from dbbasic_content import ContentDB
9
10app = Flask(__name__)
11content = ContentDB('/var/app/content')
12
13
14@app.route('/')
15def index():
16 """Homepage - list recent posts."""
17 posts = content.get_posts(status='published', limit=10)
18 return render_template('index.html', posts=posts)
19
20
21@app.route('/<slug>/')
22def post(slug):
23 """Individual post page."""
24 post = content.get_post(slug)
25
26 if not post:
27 abort(404)
28
29 return render_template('post.html', post=post)
30
31
32@app.route('/category/<category>/')
33def category(category):
34 """Posts by category."""
35 posts = content.get_posts(categories=[category])
36 return render_template('category.html', category=category, posts=posts)
37
38
39@app.route('/tag/<tag>/')
40def tag(tag):
41 """Posts by tag."""
42 posts = content.get_posts(tags=[tag])
43 return render_template('tag.html', tag=tag, posts=posts)
44
45
46@app.route('/search/')
47def search():
48 """Search posts."""
49 from flask import request
50 query = request.args.get('q', '')
51
52 if not query:
53 return render_template('search.html', results=[], query='')
54
55 results = content.search(query)
56 return render_template('search.html', results=results, query=query)
57
58
59if __name__ == '__main__':
60 app.run(debug=True)