#manage.py #coding=utf-8 #Flask-Script是一个可以在flask应用外部编写脚本的扩展 #常用功能: #运行一个开发的服务器 #python shell中操作数据库看 #命令行任务 #from flask_script import Manager #执行语句 python manage.py save from flask_script import Manager from app import app, db from models import User manager = Manager(app) @manager.command def save(): user = User("dadao3", "dadao3@123.com") user.save() @manager.command def query_users(): users = User.objects.all() for u in users: print u if __name__ == "__main__": manager.run() #models.py #coding=-utf-8 from app import db class User(db.Document): name = db.StringField() email = db.StringField() def __str__(self): return "name: %s--email: %s" % (self.name, self.email) #app.py #coding=utf-8 # pip install flask-mongoengine from flask import Flask from flask.ext.mongoengine import MongoEngine app = Flask(__name__) app.config["MONGODB_SETTINGS"] = {"db":"users"} db = MongoEngine(app) @app.route("/") def main(): return "hello world" if __name__ == "__main__": app.run(debug=True)