标签: mongoalchemy

如何为 python Flask 应用程序提供结构

我是 python 烧瓶的新手

在单个文件中使用 MongoDB 试验一些端点,如下所示

from flask import Flask, request
from flask.ext.mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['MONGOALCHEMY_DATABASE'] = 'library'
db = MongoAlchemy(app)


class Author(db.Document):
    name = db.StringField()


class Book(db.Document):
    title = db.StringField()
    author = db.DocumentField(Author)
    year = db.IntField();


@app.route('/author/new')
def new_author():
    """Creates a new author by a giving name (via GET parameter)
    e.g.: GET /author/new?name=Francisco creates a author named Francisco
    """
    author = Author(name=request.args.get('name', ''))
    author.save()
    return 'Saved :)'






@app.route('/authors/')
def list_authors():
    """List all authors. …
Run Code Online (Sandbox Code Playgroud)

python mongodb flask mongoalchemy

2
推荐指数
1
解决办法
3723
查看次数

标签 统计

flask ×1

mongoalchemy ×1

mongodb ×1

python ×1