Django新版本3.1,设置文件有一些更改 [英] Django new version 3.1, the settings file have some changes

查看:74
本文介绍了Django新版本3.1,设置文件有一些更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django新版本3.1上,设置文件进行了一些更改,我问我如何继续设置静态文件?我通常的工作方式并没有更多用.

On Django new version 3.1, the settings file have some changes, and I came to ask how I have to proceed to set my static files? The way that I usually did doesn't work more.

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

版本3.1:

from pathlib import Path
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent

我通常这样设置我的静态文件:

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')

如果我插入 import os 会起作用,但这是正确的做法吗?设置此最佳做法是什么?谢谢吗?

If I insert the import os will work, but is it the right practice? What is the best practice to set this? Thank you?

推荐答案

此更改使您轻松定义 STATIC MEDIA 变量.为此,您甚至不需要导入 os ,而只需将以下代码添加到 settings.py :

This change makes it a lot easier for you to define your STATIC and MEDIA variables. You don't even need to import os for this purpose and all you need is to add following codes to your settings.py:

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent # which shows the root directory of your project

STATIC_ROOT = BASE_DIR / 'static' # is equal to os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'

MEDIA_ROOT = BASE_DIR / 'media' # is equal to os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

这篇关于Django新版本3.1,设置文件有一些更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆