django合集

使用mysql

环境

  • django 2.1.7
  • pymysql 0.9.3

django2.2兼容问题

会出现pumysql 兼容问题,所以降级到2.1.7

File "/usr/local/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

安装Miniconda3

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

使用django-environ

使用这个可以创建一个.env文件把敏感信息作为配置文件

使用django-redis

使用gunicorn

项目目录

.
├── README.MD
├── cloudflare_scrape
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── cloudflare_scrape.conf
├── db.sqlite3
├── docs
├── gunicorn.conf.py
├── logs
├── manage.py
└── requirements.txt

安装gunicorn和gevent

pip install gunicorn gevent

不带配置文件启动

gunicorn --bind 0.0.0.0:10000 cloudflare_scrape.wsgi:application

gunicorn配置文件gunicorn.conf.py

import os
from multiprocessing import cpu_count

bind=["127.0.0.1:10000"]
proc_name = "cloudflare_scrape"
daemon = False
workers = cpu_count()*2
worker_class = "gevent"
forworded_allow_ips = '*'
keepalive = 6
timeout = 65
graceful_timeout = 10
worker_connections = 65535
accesslog = "logs/access.log"
errorlog = "logs/error.log"
access_log_format = '%(h)s %(t)s "%(r)s" %(s)s %(b)s'

supervisor配置文件

[program:cloudflare_scrape]
process_name=%(program_name)s_%(process_num)02d
directory=/data/www/cloudflare_scrape
command=/home/server/miniconda3/bin/gunicorn -c gunicorn.conf.py cloudflare_scrape.wsgi:application
autostart=true
autorestart=true
startretires=3
user=server
redirect_stderr=true
stdout_logfile=/var/log/supervisor/cloudflare_scrape.log

使用pymysql+orm

安装pymysql

pip install pymysql

init.py 设置msyql连接

"""
setting中的配置默认为sqlite3数据库 当需要修改成MySql时
并且在setting.py的同级目录的__init__.py 加入如下配置
否则会报错: Error loading MySQLdb module.
"""
import pymysql
pymysql.install_as_MySQLdb()

设置models

这里我没有写到models.py文件中,而是 自己新建了个目录 models 然后每个table都是一个py文件

from __future__ import unicode_literals

from django.db import models

class Spot(models.Model):
    # 注意这里一定要使用 AutoField 否则你使用orm是不会返回主键id的
    id = models.AutoField(primary_key=True)
    platform = models.CharField(max_length=16)
    source = models.CharField(max_length=32)
    target = models.CharField(max_length=32)
    symbol = models.CharField(max_length=32)
    created_at = models.IntegerField()
    updated_at = models.IntegerField()
    deleted_at = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'spot'

查询

Spot.objects.all()