第3课 django 学生管理系统-基本配置

配置内容在setting.py文件

1、修改时区及语言

LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'

2、文件存储设置

# 文件储存根路径
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads/')
# 文件访问url
MEDIA_URL = '/uploads/'

在 urls.py 里面增加路由

re_path(r'^uploads/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT})

其中 uploads 与setting配置的访问URL一致

在 admin.py 里面增加显示

from django.utils.html import format_html

list_display = (... , get_photo )


def get_photo(self, obj):
    return format_html('<img src="{}" width="50" height="50" />'.format(f'/uploads/{obj.photo}'))

get_photo.short_description = '照片'

3、编写其他模块

完整代码参考 https://gitee.com/lkyuan4/student

This entry was posted in 应用. Bookmark the permalink.

发表评论