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

如何为python flask应用程序提供结构,python,mongodb,flask,mongoalchemy,Python,Mongodb,Flask,Mongoalchemy,我是新来的 在单个文件中使用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(d

我是新来的

在单个文件中使用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()
我应该能够在模式定义和使用的不同文件中获得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
@app.route('/author/new')
def new_author():
“”“通过给定名称创建新作者(通过GET参数)
e、 g.:GET/author/new?name=Francisco创建一个名为Francisco的作者
"""
author=author(name=request.args.get('name','')
author.save()
返回“已保存:)”
@app.route(“/authors/”)
def list_authors():
“”“列出所有作者。
e、 g.:获取/删除
authors=Author.query.all()
content='作者:

' 对于作者中的作者: content+='%s

'%author.name 返回内容
在端点文件中,我应该获得数据库模式的引用,请帮助我获取此结构


请给我指一些可以帮助我理解的示例或视频,我对python和flask都是新手,请给我指一些示例并帮助我了解更多信息,谢谢

基本结构可能如下所示:

/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/\u init\upy:初始化创建Flask实例的应用程序。(也使您的应用程序成为一个软件包)

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。包含url端点和关联的行为

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
从flask导入蓝图
从.models导入作者
author\u bp=Blueprint('author',\uuuuu name\uuuuuuuuu)
@作者路径('/author/new')
def new_author():
“”“通过给定名称创建新作者(通过GET参数)
e、 g.:GET/author/new?name=Francisco创建一个名为Francisco的作者
"""
author=author(name=request.args.get('name','')
author.save()
返回“已保存:)”
@作者路径(“/authors/”)
def list_authors():
“”“列出所有作者。
e、 g.:获取/删除
authors=Author.query.all()
content='作者:

' 对于作者中的作者: content+='%s

'%author.name 返回内容
yourapp/static/…包含静态文件

yourapp/templates/。包含您的模板

requirements.txt提供了包依赖关系的快照

venv(Virtualenv)文件夹,在该文件夹中,您的python库能够在包含的环境中工作

参考资料:


@Beat我已经更新了我的答案,包括对结构的更广泛概述。请再次检查。我遵循的目录与上面相同,但当我运行run.py时,我得到的错误有回溯(最近一次调用):文件“run.py”,第1行,在from flaskApp import create_app File“D:\angular python\pythonAppStructure\flaskApp_init_u.py”,第3行,在从配置导入配置导入错误:无法导入名称配置请说明我在做什么wrong@dhanalakshmi尝试将其更改为“从配置导入配置”(第二个配置,大写字母)谢谢,是的,我试过了,我知道它来自文件名导入类名,但我仍然得到了相同的错误,你能为上面的目录结构创建一个git repo吗?这会对我有很大帮助吗?我得到的错误有回溯(最近一次调用):文件“run.py”,第3行,在app=create_app()文件中“D:\angular python\pythonAppStructure\flaskApp\u init\u.py”,在create\u app.config.from\u object(config)NameError中的第8行:未定义全局名称“config”
from . import db

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


class Book(db.Document):
    title = db.StringField()
    author = db.DocumentField(Author)
    year = db.IntField();
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