在有了一定的學習和工作經驗之後,決定重寫我之前的博客。
- 項目地址:https://github.com/FesonX/RebuildBlog
第一天的代碼可以 checkout 這個 commit
4d48cc734e042fb531b0e625cbc09fab3b4e8409
配置 PyCharm
定製 Python 文件頭部
定製完文件頭部會有種真正擁有這份代碼的感覺,配置路徑如下:
Pycharm Python 模板支持識別以下變量
${PROJECT_NAME} - the name of the current project.
${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
${USER} - the login name of the current user.
${DATE} - the current system date.
${TIME} - the current system time.
${YEAR} - the current year.
${MONTH} - the current month.
${DAY} - the current day of the month.
${HOUR} - the current hour.
${MINUTE} - the current minute.
${PRODUCT_NAME} - the name of the IDE in which the file will be created.
${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.
可以自由選擇組合的變量,定製自己的模板,以下是我的模板示例:
# -*- coding:utf-8 _*-
"""
@file: ${NAME}.py
@time: ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE}
@author:FesonX
@contact: [email protected]
@site: github.com/FesonX
@software: ${PRODUCT_NAME}
"""
虛擬環境配置
配置加速鏡像源
根據清華源的配置指引,先確認你的 pip 版本是否大於 10.0
$ pip --version
# 顯示示例
pip 19.0.3 from /path/to/pip (python 3.7)
配置爲默認源
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
驗證配置效果
找一個庫如 SQLAlchemy,看看安裝輸出的網址是不是清華源
$ pip install sqlalchemy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting sqlalchemy
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b9/9f/326aaeda67ccbc9765551da7f9359f1bc5b33ecd3552b0c47233f94b801f/SQLAlchemy-1.3.17-cp37-cp37m-macosx_10_14_x86_64.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 543 kB/s
Installing collected packages: sqlalchemy
Successfully installed sqlalchemy-1.3.17
配置 Git
利用Git 做版本管理可以方便回溯所有更改,寫 ReleaseNote 時也可以輕鬆定位修改,以下配置參照 初次運行 Git 前的配置
配置用戶信息
這是在版本控制系統中,用來識別你的信息,--global 參數設置爲全局有效
示例
git config --global user.name "FesonX"
git config --global user.email "[email protected]"
爲項目添加 .gitigonore
Github 官方提供了一系列的 gitignore 模板,非常全面,建立一個項目時,可以先複製一份,避免誤提交如 pyc、虛擬環境文件夾 *env 等內容到項目中。
之所以說它全面,是因爲它除了考慮到 Python 常見的非代碼文件,還把一些知名的 Python 框架非代碼或敏感文件都囊括在內。如 Flask 用於存儲敏感配置的 instance 文件夾,Celery 的 pid 文件,Django 的本地配置文件,以及常用的虛擬環境文件夾等。
當然,定製化的需求還請參照 Git Ignore 文檔(英文)
目前 PyCharm(2019.3.4) 已經支持從 gitignore 中識別文件夾或文件,方便檢查忽略文件的正則有沒有填寫錯誤。
項目結構
第一版的項目結構如下,所有的 app 註冊和創建行爲都在 src/__init__.py
下完成。後續還將單獨添加路由註冊入口、路由邏輯控制器等,也將放在 src 目錄下,目前先不進行創建。
├── instance
│ └── config.py
└── src
├── __init__.py
├── models.py
├── statics
└── templates
├── app.py
├── config.py
src 相當於一個包,也可以參照 Django 以 app 作爲文件夾管理單元,Flask 官方提供了項目結構的示例,參見:Project Layout
爲博客項目創建 MySQL 用戶和數據庫
單獨創建博客用戶,方便管理,也避免其他信息從服務器泄露. 密碼可以使用 uuidgen 命令從命令行生成,或者通過 Python uuid 庫生成
-- 創建數據庫
create database rebuild_blog;
-- 創建用戶
create user blog_admin identified by 'pwd';
-- 授權
grant all privileges on rebuild_blog.* to blog_admin@'%' identified by 'pwd';
密碼生成示例:
from uuid import uuid4
password = str(uuid4())
password.replace('-', '') # Optional
>> 'bad93261324d4b6081f2be4a965b506a'
創建完畢後,爲 Flask 項目添加服務器連接配置,建議放在 instance 文件夾下。數據庫配置具體參見SQLAlchemy Engine Configuration
# 使用 mysql-client 作爲驅動
SQLALCHEMY_DATABASE_URI = 'mysql+mysqldb://blog_admin:pwd@localhost:3306/rebuild_blog'
接下來,就是設計數據庫模型了。