我试图链接一些CSS在我的html(运行Django服务器),问题是,如果在Windows上运行并使用(反斜杠),但它不会在Linux上正常执行(因为它使用/(斜杠)
根据当前执行的操作系统修改base.html中的路径的最佳方法是什么?
例如,如果是Windows,它应该是:
<link rel="stylesheet" href="{% static "style.css" %}"/>
^
但是在Linux上:
<link rel="stylesheet" href="{% static "/style.css" %}"/>
^
首先,在settings.py
中配置STATIC_ROOT
和STATIC_URL
:
import os
# Project root is intended to be used when building paths,
# e.g. ``os.path.join(PROJECT_ROOT, 'relative/path')``.
PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))
# Absolute path to the directory where ``collectstatic``
# will collect static files for deployment.
#
# For more information on ``STATIC_ROOT``, visit
# https://docs.djangoproject.com/en/1.8/ref/settings/#static-root
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
# URL to use when referring to static files.
#
# For more information on ``STATIC_URL``, visit
# https://docs.djangoproject.com/en/1.8/ref/settings/#static-url
STATIC_URL = '/static/'
那么你应该能够使用
{% static 'project/css/style.css' %}
,据我所知,它会处理兼容性。
考虑阅读有关管理静态文件的内容