本文内容皆为作者原创,如需转载,请注明出处:
一:创建表
继承AbstractUser,增加两个字段,telephone,icon(依附于pillow)
1.在user这个spp的models.py
中输入下列代码:
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
telephone = models.CharField(max_length=11)
avatar = models.ImageField(upload_to='avatar', default='avatar/default.png')
2.在settings文件夹中的dev.py
添加如下代码:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # 现在的BASEDIR是luffyapi下的luffyapi
AUTH_USER_MODEL = 'user.user'
4.放一下图片进去,命名为default.png
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}),
]