我是新來的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拉克希米試着改變它太「從配置導入配置」(第二個配置大寫字母) –