Django 2 数据库配置

1.配置模板文件

在项目根目录新建一个templates文件夹,用于存放模版文件。

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',
            ],
            # 将函数内置到模板中 https://docs.djangoproject.com/en/2.1/topics/templates/
            'builtins': ['django.templatetags.static'],
        },
    },
]

2.配置mysql数据库

mysql -uroot -p0000

  创建数据库

mysql> CREATE DATABASE mysite CHARSET=utf8;

mysql> CREATE USER lubai IDENTIFIED BY'0000';

mysql> GRANT ALL PRIVILEGES ON mysite.* TO 'lubai'@'%';
mysql> FLUSH PRIVILEGES;

DATABASES= {
   'default': {
       'ENGINE': 'django.db.backends.mysql',   # 数据库引擎
       'NAME': 'mysite',                       # 数据库名称
       'USER': 'lubai',                      # 数据库登录用户名
       'PASSWORD': '0000',                # 密码
       'HOST': '127.0.0.1',                # 数据库主机IP,如保持默认,则为127.0.0.1
       'PORT': 3306,                           # 数据库端口号,如保持默认,则为3306
  }
}

方法二
DATABASES= {
   'default': {
       'ENGINE': 'django.db.backends.mysql',
       'OPTIONS': {
           'read_default_file': 'utils/dbs/my.cnf',
      },
  }
}
(youkou_env) pyvip@VIP:~$ pip install -i https://pypi.douban.com/simple pymysql
# 在虚拟机中安装django-redis
pip install -i https://pypi.douban.com/simple django-redis
# 在settings.py文件中指定redis配置
CACHES= {
   "default": {
       "BACKEND": "django_redis.cache.RedisCache",
       "LOCATION": "redis://127.0.0.1:6379/0",
       "OPTIONS": {
           "CLIENT_CLASS": "django_redis.client.DefaultClient",
      }
  },
}

3 设置redis缓存
用于存放用户session信息、短信验证码以及图片验证码信息等。
# 在settings.py文件中指定redis配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/0",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
}

 



posted @ 2019-04-08 14:02  liubosong  阅读(428)  评论(0编辑  收藏  举报