1.配置模板文件
1 TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'templates')], 5 'APP_DIRS': True, 6 'OPTIONS': { 7 'context_processors': [ 8 'django.template.context_processors.debug', 9 'django.template.context_processors.request', 10 'django.contrib.auth.context_processors.auth', 11 'django.contrib.messages.context_processors.messages', 12 ], 13 # 将模板标签内置到模板中 https://docs.djangoproject.com/en/2.1/topics/templates/ 14 'builtins': ['django.templatetags.static'], 15 }, 16 }, 17 ]
2.配置mysql数据库
-
创建数据库
1 mysql> create database tzproject charset=utf8mb4; 2 Query OK, 1 row affected (0.00 sec)
2.创建用户
1 mysql> create user 'dj_user'@'%' identified by 'pythonvip'; 2 Query OK, 1 row affected (0.00 sec)
3.授权
1 mysql> grant all privileges on tzproject.* to 'dj_user'@'%'; 2 Query OK, 0 rows affected (0.03 sec) 3 mysql> flush privileges; 4 Query OK, 0 rows affected (0.02 sec) 5 # 授权创建一起写 6 grant all privileges on tzproject.* to 'xinlan'@'%' identified by 'pythonvip'
4.配置settings
方法一:直接在settings.py文件中添加数据库配置信息
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 4 'NAME': 'tzproject', # 数据库名 5 'USER': 'dj_user', # 用户名 6 'PASSWORD': 'pythonvip', # 密码 7 'HOST': '127.0.0.1', # 主机IP 8 'PORT': 3306 # 端口 9 } 10 }