我正在做烧瓶培训课程enter link description here
我在虚拟环境中安装Flask-SQLAlchemy。 我正在安装Flask-Migrate: pip安装flask-migrate
创建了一个类config.py:
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or '*********'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
在models.py中,我有两个模型:
from datetime import datetime
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic')
def __repr__(self):
return '<User {}>'.format(self.username)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Post {}>'.format(self.body)
仅在testapp.py中:
from app import app
在文件 init .py
中from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models
构建我的应用程序:
testapp\
venv\
app\
templates\
base.html
index.html
login.html
__init__.py
routes.py
forms.py
models.py
testapp.py
config.py
在Python解释器会话中,执行以下操作 从app.models导入用户 并得到一个错误:
(venv) C:\Users\User\testapp>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916
64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from app.models import User
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\User\testapp\app\__init__.py", line 4, in <module>
from config import Config
File "C:\Users\User\testapp\config.py", line 8
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
^
TabError: inconsistent use of tabs and spaces in indentation
>>>
设置新配置模块的环境变量 设置FLASK_APP = config.py
当我执行命令时:
flask db init
完整的错误日志:
(venv) C:\Users\User\testapp>flask db init
Usage: flask db init [OPTIONS]
Error: Failed to find Flask application or factory in module "config". Use "FLAS
K_APP=config:name to specify one.
(venv) C:\Users\User\testapp>
我在做什么错?帮助
答案 0 :(得分:1)
在cmd中运行此命令
export FLASK_APP=testapp.py
之前
flask db init