開發
生成項目
django-admin.py startproject <name>
- 生成新的app:
python manage.py startapp <appname>
django 運行項目:
python manage.py runserver 111.231.82.45:8000
python manage.py migrate
生成超級用戶
python manage.py createsuperuser
mima:f15114826978f
更新數據庫
- python manage.py makemigrations
- python manage.py migrate
- {% load staticfiles %} 顯示出圖片
html排版 alt+command+l
url映射
通過父Url 中添加url文件,映射到子url
name 名稱決定在template中跳轉的名稱
認證
應該有一個全局變量的user來做認證
user.is_authenticated
部署環境
安裝 venv
sudo pip2 install virtualenv
virtualenv VENV
指定版本
virtualenv TEST --python=python2.7
pip install django==1.9.7
sudo -H pip install
啓動虛擬環境
cd ENV
source ./bin/activate
django版本
import django
print django.VERSION
python 路徑
系統自帶:
/System/Library/Frameworks/Python.framework/Versions/2.7
django pip 安裝包
django-admin startproject mysite
pip install -r req.txt 安裝txt中的包
pip freeze > req.txt 導出pip中的內容
讓項目models 關聯到 sqlite
python manage.py makemigrations mainsite
wsgi +nginx啓動項目
wsgi 通過配置文件運行django項目:
uwsgi –ini uwsgi.ini
socket 是用來給服務器響應的
http 是直接wsgi自己服務器
訪問靜態文件
在 uwsgi.ini文件中加 daemonize = uwsgi.log 可以讓log信息保存在 uwsgi.log文件中
運行部署
gunicorn+nginx啓動項目
關於進程:
- control+c只會 掛起進程,並不會殺死進程
通過lsof -i:8000 - 找到pid 然後 通過
- kill -9 91215
修改nginx 配置文件
http : 協議類型和端口號
processes : 開啓的進程數量
workers : 開啓的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)
chdir : 指定運行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上,開啓狀態服務(enable the stats server on the specified address)
threads : 運行線程。由於GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允許主進程存在(enable master process)
daemonize : 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。
pidfile : 指定pid文件的位置,記錄主進程的pid號。
vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
打包發佈
python setup.py sdist