如何使用正确的python文件设置flask_app?

时间:2019-05-06 13:03:10

标签: python flask

我在运行烧瓶应用程序时遇到问题。我已经在命令行上设置了flask_app,但仍然收到以下消息:

  

flask.cli.NoAppException:无法导入“ testapp.app”

这是文件夹结构:

folder structure
folder structure 2

这些是文件夹中的文件:

app.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app():
    """Construct the core application."""
    app = Flask(__name__, instance_relative_config=False)
    db.init_app(app)
    app.config.from_object('config.Config')

    with app.app_context():
        # Imports
        from . import routes

        # Create tables for our models
        db.create_all()

        return app

routes.py:

from flask import request, render_template
from datetime import datetime as dt
from flask import current_app as app
from .models import db, User


@app.route('/', methods=['GET'])
def entry():
    """Endpoint to create a user."""
    new_user = User(username='myuser',
                    email='myuser@example.com',
                    created=dt.now(),
                    bio="In West Philadelphia born and raised, on the playground is where I spent most of my days",
                    admin=False
                    )
    db.session.add(new_user)
    db.session.commit()
    users = User.query.all()
    return render_template('users.html', users=users, title="Show Users")

models.py:

from . import db

class User(db.Model):
    """Model for user accounts."""

    __tablename__ = 'users'
    id = db.Column(db.Integer,
                   primary_key=True
                   )
    username = db.Column(db.String(64),
                         index=False,
                         unique=True,
                         nullable=False
                         )
    email = db.Column(db.String(80),
                      index=True,
                      unique=True,
                      nullable=False
                      )
    created = db.Column(db.DateTime,
                        index=False,
                        unique=False,
                        nullable=False
                        )
    bio = db.Column(db.Text,
                    index=False,
                    unique=False,
                    nullable=True
                    )
    admin = db.Column(db.Boolean,
                      index=False,
                      unique=False,
                      nullable=False
                      )...

config.py:

import os

class Config:
    """Set Flask configuration vars from .env file."""

    # General
    TESTING = os.environ["TESTING"]
    FLASK_DEBUG = os.environ["FLASK_DEBUG"]

    # Database
    SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI")
    SQLALCHEMY_TRACK_MODIFICATIONS = os.environ.get("SQLALCHEMY_TRACK_MODIFICATIONS")

#__ init __。py:

empty file

感谢您的帮助。

0 个答案:

没有答案