- 没有数据表被创建的依据是查看数据库里,没有任何数据表生成。
- 数据库配置如下:
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root'
PASSWORD = 'root'
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'test_blog'
SQLALCHEMY_DATABASE_URI = '{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False
- 我又按照您书里面的思路去写了
__init__.py
文件,代码如下:
import click
from flask import Flask
from main.admin import admin_bp
from main.front import index_bp
from exts import db,migrate
from model import User, Post, Category, Comment, Tags
import config
def create_app():
app = Flask(__name__)
app.config.from_object(config)
register_extensions(app)
register_blueprints(app)
register_commands(app)
register_shell_context(app)
return app
def register_extensions(app):
db.init_app(app)
migrate.init_app(app, db)
def register_blueprints(app):
app.register_blueprint(index_bp)
app.register_blueprint(admin_bp, url_prefix='/admin')
def register_shell_context(app):
@app.shell_context_processor
def make_shell_context():
return dict(db=db, User=User, Post=Post, Category=Category, Comment=Comment)
def register_commands(app):
@app.cli.command()
@click.option('--drop', is_flag=True, help='Create after drop.')
def initdb(drop):
"""Initialize the database."""
if drop:
click.confirm('This operation will delete the database, do you want to continue?', abort=True)
db.drop_all()
click.echo('Drop tables.')
db.create_all()
click.echo('Initialized database.')
@app.cli.command()
@click.option('--category', default=10, help='Quantity of categories, default is 10.')
@click.option('--post', default=50, help='Quantity of posts, default is 50.')
@click.option('--comment', default=500, help='Quantity of comments, default is 500.')
def forge(category, post, comment):
"""Generate fake data."""
from fake import fake_user, fake_category, fake_post
db.drop_all()
db.create_all()
click.echo('Generating the administrator...')
fake_user()
click.echo('Generating %d categories...' % category)
fake_category(category)
click.echo('Generating %d posts...' % post)
fake_post(post)
click.echo('Done.')
能够运行,能够访问蓝本路由,但是在Terminal
中执行flask forge
时提示如下:
(venv) H:\New_test>flask forge
Usage: flask [OPTIONS] COMMAND [ARGS]...
Try "flask --help" for help.
Error: No such command "forge".
运行flask --help
的结果如下:
(venv) H:\New_test>flask --help
Traceback (most recent call last):
File "h:\new_test\venv\lib\site-packages\flask\cli.py", line 556, in list_commands
rv.update(info.load_app().cli.list_commands(ctx))
File "h:\new_test\venv\lib\site-packages\flask\cli.py", line 399, in load_app
"Could not locate a Flask application. You did not provide "
flask.cli.NoAppException: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the curren
t directory.
Usage: flask [OPTIONS] COMMAND [ARGS]...
A general utility script for Flask applications.
Provides commands from Flask, extensions, and the application. Loads the
application defined in the FLASK_APP environment variable, or from a
wsgi.py file. Setting the FLASK_ENV environment variable to 'development'
will enable debug mode.
> set FLASK_APP=hello.py
> set FLASK_ENV=development
> flask run
Options:
--version Show the flask version
--help Show this message and exit.
Commands:
db Perform database migrations.
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.
请问您,这是我哪里设置问题吗?
为了便于您查阅,我将其他.py文件代码张贴出来。
exts.py
代码:
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()
migrate = Migrate()
fake.py
代码:
from model import User,Category,Post
from faker import Faker
import random,requests
from exts import db
fake = Faker('zh_CN')
#生成虚拟【管理员】和【会员】数据
def fake_user(count=10):
avatar_img = requests.get('https://api.uomg.com/api/rand.avatar').url
admin=User(user_email='admin@qq.com',
user_password='123456789',
user_nickname='Admin',
user_role='Admin',
user_avatar=avatar_img,
user_regip='127.0.0.1'
)
db.session.add(admin)
for i in range(count):
user_avatar_img = requests.get('https://api.uomg.com/api/rand.avatar').url
user = User(
user_email=fake.ascii_email(),
user_password=fake.password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True),
user_nickname=fake.name(),
user_role='User',
user_avatar=user_avatar_img,
user_regip=fake.ipv4(network=False, address_class=None, private=None)
)
db.session.add(user)
db.session.commit()
#生成虚拟【文章分类】数据
def fake_category(count=10):
category = Category(category_name='默认',category_aka='dafault')
db.session.add(category)
for i in range(count):
current_category = fake.word()
category = Category(category_name=current_category,category_aka=current_category)
db.session.add(category)
db.session.commit()
#生成虚拟【文章】数据
def fake_post(count=50):
for i in range(count):
post = Post(post_title=fake.sentence(),
post_content=fake.text(2000),
category_id=Category.query.get(random.ranint(1,Category.query.count())),
user_id=User.query.get(random.randint(1,10)),
post_thumb=fake.image_url(width=300, height=300),
post_url=fake.url(schemes=None),
publish_datetime=fake.date(pattern="%Y-%m-%d %H:%M:%S",end_datetime=None)
)
db.session.add(post)
db.session.commit()
model.py
代码如下:
from exts import db
from datetime import datetime
#先定义文章-标签多对多关联表
post_tags = db.Table('the_post_tags',
db.Column('post_id',db.Integer,db.ForeignKey('post.post_id'),primary_key=True),
db.Column('tags_id',db.Integer,db.ForeignKey('tags.tag_id'),primary_key=True),extend_existing=True
)
class User(db.Model):
__tablename__='user'
user_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
user_email = db.Column(db.String(128),nullable=True,unique=True)
user_password = db.Column(db.String(128),nullable=True)
user_nickname = db.Column(db.String(128),nullable=True,unique=True)
user_role = db.Column(db.String(48),nullable=True)
user_regdate = db.Column(db.DateTime,default=datetime.now())
user_regip = db.Column(db.String(128),nullable=True)
user_avatar = db.Column(db.Text,nullable=True,default='https://api.btstu.cn/sjtx/api.php?lx=c1&format=images')
posts = db.relationship('Post',backref='author') #作者文章一对多关系
comments = db.relationship('Comment',backref='commenteby') #用户评论一对多关系
class Post(db.Model):
__tablename__='post'
post_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
post_title = db.Column(db.String(256),nullable=False)
post_thumb = db.Column(db.Text,nullable=False,default='https://api.uomg.com/api/rand.avatar?sort=女&format=jpg')
publish_datetime = db.Column(db.DateTime,default=datetime.now())
post_content = db.Column(db.Text,nullable=False)
post_url = db.Column(db.String(256),nullable=False)
post_view = db.Column(db.Integer,nullable=False,default=0)
tag_id = db.relationship('Tags', secondary=post_tags, backref=db.backref('posts')) #文章-标签多对多关系
category_id = db.Column(db.Integer, db.ForeignKey('category.category_id')) #文章分类一对多关系
category = db.relationship('Category', back_populates='category')
user_id = db.Column(db.Integer, db.ForeignKey('user.user_id')) #作者文章一对多关系
comments = db.relationship('Comment',backref='thepost',cascade='all,delete-orphan') #文章评论一对多关系
class Category(db.Model):
__tablename__='category'
category_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
category_name = db.Column(db.String(256),nullable=False,unique=True)
category_aka = db.Column(db.String(256),nullable=False,unique=True)
parent_category_id = db.Column(db.Integer)
posts = db.relationship('Post',back_populates='thecategory') #文章分类一对多关系
class Comment(db.Model):
__tablename__ = 'comment'
comment_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
parent_comment_id = db.Column(db.Integer)
comment_datetime = db.Column(db.DateTime,default=datetime.now())
comment_content = db.Column(db.Text,nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.user_id')) #用户评论一对多关系
post_id = db.Column(db.Integer,db.ForeignKey('post.post_id')) #文章评论一对多关系
post = db.relationship('Post',back_populates='comments')
class Tags(db.Model):
__tablename__ = 'tags'
tag_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
tag_name = db.Column(db.String(256),nullable=False)
tag_aka = db.Column(db.String(256),nullable=False)
db.create_all()
最后,十分感谢您在百忙之中抽出时间给出的指导意见,谢谢!