django、drf相关配置

╄→гoц情女王★ 提交于 2020-02-25 19:38:39

国际化配置

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

自定义User表

models.py
class User(AbstractUser):
    mobile = models.BigIntegerField(unique=True,null=True)

    def __str__(self):
        return self.username
settings.py
AUTH_USER_MODEL = 'api.USer'
admin.py
from django.contrib import admin
from . import models

from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin

# 自定义User表后,admin界面管理User类
class UserAdmin(DjangoUserAdmin):
    # 添加用户课操作字段
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'is_staff', 'mobile', 'groups', 'user_permissions'),
        }),
    )
    # 展示用户呈现的字段
    list_display = ('username', 'mobile', 'is_staff', 'is_active', 'is_superuser')


admin.site.register(models.User, UserAdmin)
admin.site.register(models.Book)
admin.site.register(models.Car)

pip换源

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

MEDIA相关配置

urls.py
from django.views.static import serve

urlpatterns = [
    url(r'^media/(?P<path>.*)', serve, {"document_root": settings.MEDIA_ROOT})
]
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

数据库相关配置

settings.py
import pymysql

pymysql.install_as_MySQLdb()
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'day72',
        'USER': 'root',
        'PASSWORD': '',
        'HOST':'127.0.0.1',
        'PORT':3306
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!