基于当前操作系统的Python构建路径[css导入]

时间:2015-07-22 08:54:49

标签: css linux django windows

我试图在我的html(运行Django服务器)中链接一些CSS,问题是如果在Windows上运行并使用\(反斜杠)但是它在Linux上无法正常执行(因为它使用/ (斜线)

根据当前正在执行的操作系统修改base.html中路径的最佳方法是什么?

例如,如果它是Windows,它应该是:

<link rel="stylesheet" href="{% static "\style.css" %}"/>
                                        ^

但是在Linux上:

<link rel="stylesheet" href="{% static "/style.css" %}"/>
                                        ^

1 个答案:

答案 0 :(得分:1)

首先,在example = [1., 2., 3., 4., 5., 6.] for test in TESTS: example = filter_with_prints(example, test) 中配置STATIC_ROOTSTATIC_URL

settings.py

然后你应该可以使用

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/'

据我所知,它将兼顾兼容性。

考虑阅读managing static files