我是新来的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.
e.g.: GET /authors"""
authors = Author.query.all()
content = '<p>Authors:</p>'
for author in authors:
content += '<p>%s</p>' % author.name
return content
if __name__ == '__main__':
app.run()
上面的代码包含两个端点后如下图所示并获得其工作正常数据
知道寻找一种方式将代码分成不同的文件中像
数据库连接RELAT编辑代码应该是在不同的文件
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)
我应该能够得到在不同的文件DB查询在架构定义和使用它
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.
e.g.: GET /authors"""
authors = Author.query.all()
content = '<p>Authors:</p>'
for author in authors:
content += '<p>%s</p>' % author.name
return content
在端点文件中,我应该得到数据库模式的参考,请帮助我获取此结构
点我的一些理解样品或视频,可以帮我做的,我是新来的蟒蛇以及烧瓶请点一些样本,并有助于了解更多的感谢
@Beat \t 我已经更新了我的答案,包括更广泛的结构可以是什么样子的概述。请再次查看。 –
我与上面相同的目录,但是当我运行run.py我得到的错误有回溯(最近呼叫最后): 文件“run.py”,第1行,在 from flaskApp导入create_app 文件“d:\角蟒\ pythonAppStructure \ flaskApp \ __ init__.py”,3号线,在 从配置导入配置 导入错误:无法导入名称配置 请什么我是个做错了 –
@dhana拉克希米试着改变它太“从配置导入配置”(第二个配置大写字母) –