[英]How to load static files in python django
我试图在settings.py文件中进行修改,以允许加载css文件,但是某些html代码仍未显示其样式,请在终端中运行命令“ python manage.py collectstatic”,并说文件是复制,但仍未显示其效果。
这是settings.py文件中的修改行
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
在您的settings.py中提供:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = 'http://example.com/static/'
因此,如果定义了静态文件查找器,并且将static_files声明为静态文件的源,则无论放置在哪里
/your_root/static_files/
将进入
/your_root/static
跑步后
python manage.py collectstatic
与错误
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
是您要尝试从中获取并带到同一目录,该目录是所收集的所有静态文件的目标目录。
最好设置STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
如果在BASE_DIR中有一个名为static
的文件夹,请添加
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
您可以在运行python manage.py collectstatic
之后将static_root/
添加到.gitignore
文件中
我使用的方式是:
2.在HTML
首先加载静态文件{%加载静态%}
然后链接到路径
href =“ {%static'name_of_app / fileName'%}”
您可以在最新的Django版本中将这些代码添加到settings.py中
STATIC_URL ='/静态/'
MEDIA_URL ='/ media /'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH,'static_root')
MEDIA_ROOT = os.path.join(VENV_PATH,'media_root')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.