virtualenv jamesharding-flask-blog
cd jamesharding-flask-blog
source bin/activate
nano requirements.txt
输入以下内容:
Flask==0.10.1
Flask-FlatPages==0.5
Frozen-Flask==0.11
Jinja2==2.7.1
Markdown==2.3.1
MarkupSafe==0.18
PyYAML==3.10
Pygments==1.6
Werkzeug==0.9.3
itsdangerous==0.22
wsgiref==0.1.2
pip install -r requirements.txt
mkdir content static templates
mkdir content/posts
nano blog.py
blog.py的内容为
import sys
from flask import Flask, render_template
from flask_flatpages import FlatPages, pygments_style_defs
from flask_frozen import Freezer
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
FLATPAGES_ROOT = 'content'
POST_DIR = 'posts'
app = Flask(__name__)
flatpages = FlatPages(app)
freezer = Freezer(app)
app.config.from_object(__name__)
@app.route("/")
def posts():
posts = [p for p in flatpages if p.path.startswith(POST_DIR)]
posts.sort(key=lambda item:item['date'], reverse=False)
return render_template('posts.html', posts=posts)
@app.route('/<name>/')
def post(name):
path = '{}/{}'.format(POST_DIR, name)
post = flatpages.get_or_404(path)
return render_template('post.html', post=post)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
app.run(host='0.0.0.0', debug=True)
cd templates
在templates/下,新建posts.html,内容为:
{% for post in posts %}
<a href="{{ url_for('post', name=post.path.replace('posts/', '')) }}">
<p>{{ post.title }}</p>
</a>
<small>{{ post.date }}</small>
{% endfor %}
在templates/下,新建post.html,内容为:
{{ post.date }}
<p><h2>{{ post.title }}</h2></p>
{{ post.html|safe }}
发贴方法:
cd ../content/posts/
nano test1.md
内容格式为:
title: test1
date: 2014-01-04
这是测试1.
然后,
cd ~/jamesharding-flask-blog/
as3:~/jamesharding-flask-blog# ls
bin blog.py content include lib requirements.txt static templates
as3:~/jamesharding-flask-blog# python blog.py build (这个就是生成/更新静态网站的命令)
as3:~/jamesharding-flask-blog# ls
bin blog.py build content include lib requirements.txt static templates
(新出现了build目录)
as3:~/jamesharding-flask-blog# cd build
as3:~/jamesharding-flask-blog/build# ls
chinese-economy fengyuwuzu index.html test1 test2
as3:~/jamesharding-flask-blog/build# nohup Rwebserver 23184 > /dev/null &
可见~/jamesharding-flask-blog/build/就是静态网站的根目录。
演示站点:http://as3.brite.biz:23184/,http://jfb.brite.biz/
from http://www.jamesharding.ca/posts/simple-static-markdown-blog-in-flask/
原作者其实有2处写错了:
@app.route("/posts/") 应该改为 @app.route("/")
@app.route('/posts/<name>/') 应该改为 @app.route('/<name>/')
cd jamesharding-flask-blog
source bin/activate
nano requirements.txt
输入以下内容:
Flask==0.10.1
Flask-FlatPages==0.5
Frozen-Flask==0.11
Jinja2==2.7.1
Markdown==2.3.1
MarkupSafe==0.18
PyYAML==3.10
Pygments==1.6
Werkzeug==0.9.3
itsdangerous==0.22
wsgiref==0.1.2
pip install -r requirements.txt
mkdir content static templates
mkdir content/posts
nano blog.py
blog.py的内容为
import sys
from flask import Flask, render_template
from flask_flatpages import FlatPages, pygments_style_defs
from flask_frozen import Freezer
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
FLATPAGES_ROOT = 'content'
POST_DIR = 'posts'
app = Flask(__name__)
flatpages = FlatPages(app)
freezer = Freezer(app)
app.config.from_object(__name__)
@app.route("/")
def posts():
posts = [p for p in flatpages if p.path.startswith(POST_DIR)]
posts.sort(key=lambda item:item['date'], reverse=False)
return render_template('posts.html', posts=posts)
@app.route('/<name>/')
def post(name):
path = '{}/{}'.format(POST_DIR, name)
post = flatpages.get_or_404(path)
return render_template('post.html', post=post)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
app.run(host='0.0.0.0', debug=True)
cd templates
在templates/下,新建posts.html,内容为:
{% for post in posts %}
<a href="{{ url_for('post', name=post.path.replace('posts/', '')) }}">
<p>{{ post.title }}</p>
</a>
<small>{{ post.date }}</small>
{% endfor %}
在templates/下,新建post.html,内容为:
{{ post.date }}
<p><h2>{{ post.title }}</h2></p>
{{ post.html|safe }}
发贴方法:
cd ../content/posts/
nano test1.md
内容格式为:
title: test1
date: 2014-01-04
这是测试1.
然后,
cd ~/jamesharding-flask-blog/
as3:~/jamesharding-flask-blog# ls
bin blog.py content include lib requirements.txt static templates
as3:~/jamesharding-flask-blog# python blog.py build (这个就是生成/更新静态网站的命令)
as3:~/jamesharding-flask-blog# ls
bin blog.py build content include lib requirements.txt static templates
(新出现了build目录)
as3:~/jamesharding-flask-blog# cd build
as3:~/jamesharding-flask-blog/build# ls
chinese-economy fengyuwuzu index.html test1 test2
as3:~/jamesharding-flask-blog/build# nohup Rwebserver 23184 > /dev/null &
可见~/jamesharding-flask-blog/build/就是静态网站的根目录。
演示站点:http://as3.brite.biz:23184/,http://jfb.brite.biz/
from http://www.jamesharding.ca/posts/simple-static-markdown-blog-in-flask/
原作者其实有2处写错了:
@app.route("/posts/") 应该改为 @app.route("/")
@app.route('/posts/<name>/') 应该改为 @app.route('/<name>/')