帶評論模板的項目:
博客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://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:password@hostname/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' |
判斷是否爲空
成功發佈新聞與評論: