flask 新闻管理系统搭建试验

时间:2024-03-21 22:07:02

 

带评论模板的项目:

博客https://github.com/goalong/flask-demo

https://github.com/13697165025/FlaskMyBlog

*新闻https://github.com/blue-harddisk/flask

一个新闻web,具体功能:登录注册,首页有分类,新闻详情页面有点赞,评论,回复评论功能,个人中心,后台管理

新建news_website的py3.7虚拟环境

activate

使用requirements安装:

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install -r requirements.txt -i http://pypi.douban.com/simple

pip install -r requirements.txt -i http://pypi.hustunique.com/simple

pip install -r requirements.txt -i http://pypi.sdutlinux.org/simple

pip install -r requirements.txt -i http://pypi.mirrors.ustc.edu.cn/simple

MarkupSafe无法正常安装

报错:

Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

cannot import name 'Feature' from 'setuptools'

setuptool版本的问题,python3源中的setuptools已经升级到46以上。所以导致pip安装失败.参考:https://blog.csdn.net/pengshengege/article/details/105113561

pip install --upgrade pip setuptools==45.2.0

报错:

Failed to build mysqlclient

无法打开包括文件: “mysql.h”: No such file or directory

参考:

https://blog.csdn.net/weixin_30564785/article/details/99818573?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://segmentfault.com/a/1190000016563585

https://blog.csdn.net/cn_1937/article/details/81533544?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

感觉都很麻烦,最后把它从requirement中删除了,运行时自动装上了最新版mysqlclient

使用migrations迁移数据库

https://www.cnblogs.com/senlinyang/p/8387007.html

为了导出数据库迁移命令,Flask-Migrate 提供了一个MigrateCommand 类,可附加到Flask-Script 的manager 对象上。在这个例子中,MigrateCommand 类使用db 命令附加。

manager.add_command('db', MigrateCommand)

在维护数据库迁移之前,要使用init 子命令创建迁移仓库:

python hello.py db init

python manage.py mysql init

报错:Directory migrations already exists

删除文件后重新运行

migrate 子命令用来自动创建迁移脚本:

python hello.py db migrate -m "initial migration"

python manage.py mysql migrate -m "initial migration"

报错:

File "D:\ProgramData\Anaconda3\envs\news_website\lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 102, in dbapi

    return __import__('MySQLdb')

ModuleNotFoundError: No module named 'MySQLdb'

尝试安装MySQLdb

pip install mysql-python -i https://pypi.tuna.tsinghua.edu.cn/simple

报错:

_mysql.c(42): fatal error C1083: 无法打开包括文件: “config-win.h”: No such file or directory

    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

参考:https://blog.csdn.net/weixin_42840933/article/details/85274313

https://www.cnblogs.com/SH170706/p/10082987.html

下载包,从dos命令行进入下载后的文件夹,执行下面命令:

pip install mysqlclient-1.3.14-cp37-cp37m-win_amd64.whl

这个版本是对的,可以用MySQLdb模块

    spec.loader.exec_module(module)

  File "<frozen importlib._bootstrap_external>", line 728, in exec_module

  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

  File "migrations\env.py", line 87, in <module>

    run_migrations_online()

  File "migrations\env.py", line 72, in run_migrations_online

    connection = engine.connect()

Traceback (most recent call last):

  File "manage.py", line 30, in <module>

    manager.run()

配置:

SQLALCHEMY_DATABASE_URI

mysql://username:[email protected]/database

匹配数据库名与配置database名,再次运行,成功

使用db upgrade 命令把迁移应用到数据库中:

python hello.py db upgrade

python manage.py mysql upgrade

尝试启动项目报错:

redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. 由于目标计算机积极拒绝,无法连接。.

参考:https://blog.csdn.net/qq_41192383/article/details/86559296

下载并安装Redis-x64-3.0.503.msi,地址:

https://github.com/MicrosoftArchive/redis/releases

安装完成后,启动服务(找到安装路径,双击redis-cli.exe文件即可)

requirement装的redis==2.10.6为python提供的模块redis-py

启动项目:

python manage.py runserver

http://localhost:5000/

点击新闻列表时报错:

File "D:\anacondaProject\flask\info\user\views.py", line 138, in news_release

    category_list.pop(0)

IndexError: pop from empty list

判断是否为空:

users = session.query(user).filter(user.id == '234).all()#users为object列表

if(len(user) == 0):

print "数据库中没有id为234的用户‘’

else:

print "数据库中包含id为234的用户“

if(len(categorys)!=0):

            for category in categorys:

                category_list.append(category.to_dict())

            # 删除第0个元素

            category_list.pop(0)

发布报错:

File "D:\anacondaProject\flask\info\user\views.py", line 151, in news_release

    index_image = request.files.get("index_image").read()

AttributeError: 'NoneType' object has no attribute 'read'

判断是否为空

成功发布新闻与评论:

flask 新闻管理系统搭建试验