如何在多个文件中拆分Flask代码

时间:2014-04-17 02:11:46

标签: python-2.7 flask couchdb

分割下面code的最佳方式是什么?进入MVC以避免将所有内容都存档

# -*- coding: utf-8 -*-
"""
example/guestbook.py 
====================
This is an example application for Flask-CouchDB. It's a simple guestbook that
people can sign.

:copyright: 2010 Matthew "LeafStorm" Frazier
:license:   MIT/X11, see LICENSE for details (part of Flask-CouchDB)
"""
import datetime
from flask import Flask, render_template, flash, request, redirect, url_for
from flaskext.couchdb import (CouchDBManager, Document, TextField,
              DateTimeField, ViewField, paginate)


# application setup
app = Flask(__name__)


# config
COUCHDB_SERVER = 'http://localhost:5984/'
COUCHDB_DATABASE = 'example-guestbook'
SECRET_KEY = 'set this to something secret'

app.config.from_object(__name__)


# model
class Signature(Document):
doc_type = 'signature'

message = TextField()
author = TextField()
time = DateTimeField(default=datetime.datetime.now)

all = ViewField('guestbook', '''
    function (doc) {
    if (doc.doc_type == 'signature') {
        emit(doc.time, doc);
    };
    }''', descending=True)


manager = CouchDBManager()
manager.add_document(Signature)
manager.setup(app)


# views
@app.route('/')
def display():
page = paginate(Signature.all(), 5, request.args.get('start'))
return render_template('display.html', page=page)


@app.route('/', methods=['POST'])
def post():
message = request.form.get('message')
author = request.form.get('author')
if not message or not author:
    flash("You must fill in both a message and an author")
else:
    signature = Signature(message=message, author=author)
    signature.store()
    flash("Signature stored")
return redirect(url_for('display'))


if __name__ == '__main__':
app.config.from_envvar('GUESTBOOK_SETTINGS', silent=True)
app.run(debug=True)

1 个答案:

答案 0 :(得分:0)

您可以查看官方文档:http://flask.pocoo.org/docs/patterns/packages并从Github项目中获取灵感:https://github.com/salimane/flask-mvc