一:创建表

继承AbstractUser,增加两个字段,telephone,icon(依附于pillow)

1.在user这个spp的models.py中输入下列代码:

 
from django.db import models
from django.contrib.auth.models import AbstractUser


classUser(AbstractUser):
    telephone = models.CharField(max_length=11)
    avatar = models.ImageField(upload_to='avatar', default='avatar/default.png')

image-20200716212441930

image-20200716212441930

2.在settings文件夹中的dev.py添加如下代码:

 
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # 现在的BASEDIR是luffyapi下的luffyapi
AUTH_USER_MODEL = 'user.user'

image-20200716212608591

image-20200716212608591

3.在小luffyapi中创建media文件夹,再在media文件夹中创建avatar文件夹

image-20200716212739205

image-20200716212739205

4.放一下图片进去,命名为default.png

image-20200716212936302

image-20200716212936302

5.配置路由,在urls.py中输入如下代码:

 
from django.contrib import admin
from django.urls import path, re_path
from django.views.static import serve
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}),
]