我有目前正在进行的烧瓶项目。 (下面)

python -  flask 目录结构-LMLPHP

当我使用命令运行该项目时

python run.py


我收到以下错误。


  追溯(最近一次通话):
    文件“ run.py”,第1行,在
      从网站导入应用程序
  ImportError:无法导入名称“ app”


运行

from site import app
import os

app.secret_key = os.urandom(24)
port = int(os.environ.get('PORT', 5000))


if __name__ == '__main__':
    app.run(debug="True")
# app.run(host='0.0.0.0', port=port)


__init__.py

from .views import app
from .models import db


db.create_all()

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost:5432/db'
app.config['SECURITY_REGISTERABLE'] = True


views.py

from flask import Flask
from .models import User, db
from flask import render_template, request, redirect, url_for
from flask.ext.security import login_required

app = Flask(__name__, static_url_path='')

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/profile/<email>')
@login_required
def user_index(email):
    user = User.query.filter_by(email=email).first()
    return render_template('profile.html', user=user)


@app.route('/post_user', methods=['POST'])
def post_user():
    if request.form["action"] == "submit_btn":
        user = User(request.form['username'], request.form['email'])
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('index'))


models.py

from flask import Flask
from flask.ext.mail import Mail, Message
from flask_sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin

app = Flask(__name__, static_url_path='')
db = SQLAlchemy(app)
mail = Mail()
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))


目录结构应该是什么?另外,如何导入模型和视图以使服务器正常工作?

如果您需要其他信息,请告诉我,谢谢。

最佳答案

重命名站点名称,以便python不要尝试导入站点标准库,最好在init.py文件中定义应用程序:Docs

关于python - flask 目录结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36429353/

10-09 08:42