Django框架
Django命令相关
下载Django
1 | pip install django |
创建Django
1 | django-admin startproject <project-name> (可选)<IP:端口号> |
运行Django
1 | python manage.py runserver |
创建应用
1 | python manage.py startapp <app-name> |
配置应用url
1 | # urls.py |
生成迁移文件
1 | python manage.py makemigrations |
迁移文件
1 | python manage.py migrate |
DjangoMTV
模型类
字段类型
1 | BooleanField() |
字段选项
1 | primary_key # 主键 |
Meta类
- 内部Meta类来给模型赋予属性
1 | db_table # 数据表名 |
部署
uWSGI
下载uwsgi
1
pip install uwsgi
添加配置文件
1
2
3
4
5
6
7
8
9
10[uwsgi]
# http=127.0.0.1:8000
socket=127.0.0.1:8000
chdir=.../project
wsgi-file=project/wsgi.py
process=1
threads=2
pidfile=uwsgi.pid
daemonize=uwsgi.log
master=true修改setting.py
1
2DEBUG=False
ALLOWED_HOSTS=['网站域名','监听的IP']启动uwsgi
1
uwsgi --ini uwsgi.ini
查看uwsgi
1
ps aux | grep 'uwsgi'
停止uwsgi
1
uwsgi --stop uwsgi.pid
nginx
1 | location / { |
邮件告警
1 | # setting.py |
过滤敏感信息
1
2
3
4
5
6
7
8# 局部变量
# 装饰器必须放在最上方
# 不传参,过滤所有局部变量
from django.views.decorators.debug import sensitive_variables
def login(user):
password = user.password过滤POST数据
1
2
3
4
5from django.views.decorators.debug import sensitive_post_parameters
def login(request):
user = User(request.POST['username'], request.POST['password'])
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.