gpt4 book ai didi

python - Flask目录结构

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:45 26 4
gpt4 key购买 nike

我有一个目前正在进行的 flask 项目。 (下)

Flask project directory

当我使用命令运行这个项目时

python run.py

我收到以下错误。

Traceback (most recent call last): File "run.py", line 1, in from site import app ImportError: cannot import name '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)

__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'))

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

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

最佳答案

重命名站点的名称,这样 python 就不会尝试导入站点标准库,也最好在 init.py 文件中定义应用程序:Docs

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com