Django web development
django-admin startproject projectname
cd projectname
Django-admin startapp appname
Install the app: setting.py
Installed_apps[ ' appname ' ]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Url.py
from django.contrib import admin
from django.urls import path
from index.views import html_index,api,index
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',index),
path('',html_index),
path('api/', api)
]
views.py
def index(request):
return HttpResponse('hello world')
def html_index(request):
return render(request,'index.html',{'name':'Django'})
python manage.py makemigrations
python manage.py migrate
管理员 admin
python manage.py createsuperuser
Use pycharm to upload to Alibaba Cloud
Server configuration
python3.6 升级3.7
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7
将Python 3.6和Python 3.7添加到更新替代项
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
输入以下命令以配置python3:
sudo update-alternatives --config python3
键入2并按Enter键以使用Python 3.7
1.安装nginx
sudo apt-get update
sudo apt-get install nginx
2 运行(启动)nginx
nginx
3 服务器查看运行状态 或浏览器打开
curl 120.25.120.188:80
停止/重启 nginx
nginx -s stop/reload
安装uwsgi
sudo apt-get install uwsgi
安装Pip
apt install python-pip
安装Django2.2
pip3 install Django==2.2
Run Django with Gunicorn
1.安装 pip3 install gunicorn
2. Create a new file gunicorn_config.py
# -*- coding: utf-8 -*-
import multiprocessing
# 服务监听的IP和端口
bind = "0.0.0.0:8000"
# 最大挂起连接数
backlog = 2048
# 工作进程的数量,按照业务需求配置,这里设置为CPU核数*2再加1
workers = multiprocessing.cpu_count() * 2 + 1
# 工作进程的模式。
# sync对应同步模式;eventlet和gevent对应异步模式
worker_class = 'sync'
# 单个进程处理请求的线程数
threads = 1
# 单工作进程最大连接数量
worker_connections = 1000
# 工作进程在重启前累计处理的请求数,设置为0不重启
max_requests = 0
# 防止工作进程同时被重启,让各个工作进程在重启前处理的请求的数量不一致
max_requests_jitter = 0
# 工作进程超时时间
timeout = 30
# 在工作进程收到重启信号能用于处理请求的时间
graceful_timeout = 30
keep_alive = 2
# 在应用加载前切换工作目录
chdir = ""
# Gunicorn是否在后台运行
daemon = False
# 运行Gunicorn进程的用户
user = None
# 运行Gunicorn进程的用户组
group = None
# 请求日志格式
access_logformat = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s %(a)s"'
# 记录错误日志的文件
error_logfile = "-"
# 日志级别
log_level = "info"
# 下面是Gunicorn提供的钩子函数
def on_starting(server):
# 在主进程初始化之前调用
pass
def on_reload(server):
# 在工作进程收到SIGHUP信号后调用
pass
def when_ready(server):
# 在服务器启动后调用
pass
def pre_fork(server, worker):
# 在工作进程被创建前被调用
pass
def post_fork(server, worker):
# 在工作进程被创建后被调用
pass
def post_worker_init(worker):
# 在工作进程初始化应用后被调用
pass
def worker_init(worker):
# 在工作进程接收SIGINT或SIGQUIT信号,退出后被调用
pass
def worker_abort(worker):
# 在工作进程接收SIGABRT信号时被调用,一般在工作进程超时时发出SIGABRT信号
pass
def pre_exec(server):
# 在主进程创建前被调用
pass
def pre_request(worker, req):
# 在工作进程接受请求前被调用
worker.log.debug("%s %s" % (req.method, req.path))
def post_request(worker, req, environ, resp):
# 在工作进程处理请求后被调用
pass
def worker_exit(server, worker):
# 在工作进程退出时被调用
pass
def nworkers_changed(server, new_value, old_value):
# 在工作进程数量变化时被调用
pass
def on_exit(server):
# 在退出Gunicorn的时候被调用
pass
3. Run: Go to the project directory: gunicorn MyStockWeb.wsgi -c gunicorn_config.py
Pit: All print statements need to be removed, otherwise a 500 Internal Server Error will appear when calling the python program
Get the Gunicorn process tree:
pstree -ap|grep gunicorn
Restart the Gunicorn task
kill -HUP 30080
Exit the Gunicorn mission
kill -9 30080
Install MySQL
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
mysql -u root -p
#权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
使用sql语句修改root密码
use mysql;
update user set authentication_string=PASSWORD("这里输入你要改的密码") where User='root';
更新系缓存密码
update user set plugin="mysql_native_password"
刷新操作权限
flush privileges;
#创建数据库
create database mystock default charset utf8;
坑:提示解决:django中LookupError No installed app with label 'admin'
Replace Django with version 2.1.8
pip3 install django==2.1.8
pip install PyMySQL
找到mysite/mysite/init.py,在里面输入以下内容并保存:
import pymysql
pymysql.install_as_MySQLdb()