后端项目准备-下
一 创建用户表+配置媒体文件
Yanapi/apps/user/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
mobile = models.CharField(max_length=11,verbose_name='手机号')
# 需要安装pillow模块 pip install pillow
icon = models.ImageField(upload_to='icon',default='icon/default.png',verbose_name='头像',help_text='60x60')
class Meta:
db_table='yanapi_user'
verbose_name= '用户表'
verbose_name_plural =verbose_name
def __str__(self):
return self.username
Yanapi/settings/dev.py
# 自定义用户表:注册的app中的某张表
AUTH_USER_MODEL = 'user.User'
# 媒体配置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
Yanapi/urls.py
# from django.conf import settings
from settings import dev
from django.views.static import serve
urlpatterns = [
# path('/',)
path('admin/', admin.site.urls),
# 这个只负责访问
re_path(r'media/(?P<path>.*)',serve, {'document_root':dev.MEDIA_ROOT})
# 存的时候django内部admin后台会做处理
]
二 迁移数据库
准备数据库
############ 数据库配置
"""
1.管理员连接数据库
2.创建数据库
>: create database luffy default charset=utf8;
3.以管理员身份登录数据库
>: mysql -uroot -p123
4.查看用户(5.7版本mysql没有password字段,密码字段是`authentication_string`但是是加密的)
>: select user,host,password from mysql.user;
5.设置权限账号密码
# 授权账号命令:grant 权限(create, update) on 库.表 to '账号'@'host' identified by '密码'
>: grant all privileges on yanapi_db.* to 'yan'@'%' identified by '123';
# 要是本机连mysql连不上,再添加localhost访问权限,本机就可以登录了(5.7版本%不包括本机)
>: grant all privileges on yanapi_db.* to 'yan'@'localhost' identified by '123';
# 设置完有权限限制的账号后一定要刷新权限,如果没刷新权限,该终端无法被通知
>: flush privileges;
6.退出管理员,用账号 yan 密码 123 登入开发
>: exit
>: mysql -uyan -p123
"""
############ 后台项目进行数据库配置
"""
1.yanapi/settings/dev.py配置
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"HOST": "127.0.0.1",
"PORT": 3306,
"USER": "yan",
"PASSWORD": "123",
"NAME": "yanapi_db",
}
}
2.Yanapi/__init__.py
import pymysql
pymysql.install_as_MySQLdb()
"""
解决错误1
\Code\project\Yanapi> python manage.py makemigrations;
解决错误2
# 会提示安装pillow
pip intall pillow
解决错误3
\Code\project\Yanapi> python manage.py makemigrations;
\Code\project\Yanapi> python manage.py migrate;