2016-12-19 60 views
0

我是新来的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 

在端点文件中,我应该得到数据库模式的参考,请帮助我获取此结构

点我的一些理解样品或视频,可以帮我做的,我是新来的蟒蛇以及烧瓶请点一些样本,并有助于了解更多的感谢

回答

1

基本结构看起来是这样的:

/yourapp 
    /run.py 
    /config.py 
    /yourapp 
     /__init__.py 
     /views.py 
     /models.py 
     /static/ 
      /main.css 
     /templates/ 
      /base.html 
    /requirements.txt 
    /venv 

适用于您的示例它看起来像这样。

run.py:启动您的应用程序。

from yourapp import create_app 

app = create_app() 
if __name__ == '__main__': 
    app.run() 

config.py:包含的配置,你可以添加亚型开发配置,测试配置和生产配置

class Config: 
    DEBUG = True 
    MONGOALCHEMY_DATABASE = 'library' 

yourapp/__ init__.py区分:您的应用程序的初始化创建一个Flask实例。 (也使你的应用程序包)。

from flask import Flask 
from flask.ext.mongoalchemy import MongoAlchemy 
from config import Config 

db = MongoAlchemy() 

def create_app(): 
    app = Flask(__name__) 
    app.config.from_object(Config) 

    db.init_app(app) 

    from views import author_bp 
    app.register_blueprint(author_bp) 

    return app 

yourapp/models.py:包含你的不同型号。

from . import db 

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


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

yourapp/views.py:也称为routes.py有时。包含您的网址端点和相关行为。

from flask import Blueprint 
from .models import Author 

author_bp = Blueprint('author', __name__) 

@author_bp.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 :)' 

@author_bp.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 

(以下是没有必要在你的情况) yourapp /静态/ ...包含您的静态文件。

yourapp/templates/..包含您的模板。

requirements.txt具有包依赖关系的快照。

venv(Virtualenv)文件夹,您的python库可以在一个包含的环境中工作。

参考文献:

Have a look at this related question.

Good example of a widely used project structure.

+0

@Beat \t 我已经更新了我的答案,包括更广泛的结构可以是什么样子的概述。请再次查看。 –

+0

我与上面相同的目录,但是当我运行run.py我得到的错误有回溯(最近呼叫最后): 文件“run.py”,第1行,在 from flaskApp导入create_app 文件“d:\角蟒\ pythonAppStructure \ flaskApp \ __ init__.py”,3号线,在 从配置导入配置 导入错误:无法导入名称配置 请什么我是个做错了 –

+0

@dhana拉克希米试着改变它太“从配置导入配置”(第二个配置大写字母) –