Flask目录结构

时间:2016-04-05 14:31:41

标签: python flask flask-sqlalchemy flask-login

我有烧瓶项目,目前有效。 (下同)

Flask project directory

当我使用命令

运行此项目时
python run.py

我收到以下错误。

  

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

run.py

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)

__初始化__。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'))

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

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

1 个答案:

答案 0 :(得分:1)

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

http://flask.pocoo.org/docs/0.10/patterns/packages/