python - Django 1.8 CSS 加载不完全

标签 python html css django

我几乎查找了所有关于 Django CSS、JS、图像文件未加载的解决方案。但是,我认为我的元素有所不同。

我想制作一个 RWD 网站,但它在小屏幕上不起作用。 没有菜单,没有改变模板。

我可以看到 CSS 有效,但不完全有效。 image description here

尽我所能找到解决方案。我混淆了静态文件和路径,什么是正确的级别。

Here is my project path Image

我尝试了很多方法,我删除了那些没有真正涉及的方法以保持代码干净,因为有人可以帮助我。

设置.py

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templated-EX').replace('\\', '/')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

...

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'coreapp/static')

STATIC_DIRS = [
    ('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ),  
    ('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ), 
    ('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ), 
    # ('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ), 
    ]
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

index.html

{% load staticfiles %}

<!DOCTYPE HTML>
<!--
    Ex Machina by TEMPLATED
    templated.co @templatedco
    Released for free under the Creative Commons Attribution 3.0 license (templated.co/license)
-->
<html>
    <head>
        <title>Ex Machina by TEMPLATED</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:700italic,400,300,700' rel='stylesheet' type='text/css'>
        <!--[if lte IE 8]><script src="{% static "js/html5shiv.js" %}"></script><![endif]-->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="/static/js/skel.min.js"></script>
        <script src="/static/js/skel-panels.min.js"></script>
        <script src="/static/js/init.js"></script>

        <link rel="stylesheet" href="/static/css/skel-noscript.css" />
        <link rel="stylesheet" href="/static/css/style.css"/>
        <link rel="stylesheet" href="/static/css/style-desktop.css"/>

我还尝试向 urls.py 添加一些内容。也无法改进。

我是 Django 的新手,一定丢失了一些重要的东西。我卡了几天。


更新0810

好的,伙计们,

我想我比以前更清楚了。我尝试下载另一个模板并构建一个新元素。再次执行程序。

新元素没问题!所以程序是正确的。

我检查了我提到的原始模板,它与我的有点不同。 不同的是 html 和 CSS。

index.html 中的原始 CSS

<noscript>
            <link rel="stylesheet" href="{% static "css/skel-noscript.css" %}"/>
            <link rel="stylesheet" href="{% static "css/style.css" %}"/>
            <link rel="stylesheet" href="{% static "css/style-desktop.css" %}"/>
</noscript>

如果您使用 <noscript></noscript> 则它不起作用在 html 中。也许我删除了<noscript></noscript>因为 Django 不能用它运行。

最佳答案

尝试像这样更改 html 中的 href:

<link rel="stylesheet" href="{{STATIC_URL}}css/skel-noscript.css" />

如果这不起作用,请告诉我。

编辑: 您的 BASE_DIR 设置是什么? 您可以尝试以下设置吗?

# Static Files
STATIC_ROOT = join(os.path.dirname(BASE_DIR), 'staticfiles')
STATICFILES_DIRS = [join(os.path.dirname(BASE_DIR), 'static'), ]
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

关于python - Django 1.8 CSS 加载不完全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38851939/

相关文章:

python - 在 python3 中获取一个列表并生成一个字符串,但转义双引号以便作为搜索参数传递给 azure 搜索

python - 使用 sys.exit 或 SystemExit;什么时候用哪个?

php - 在 Laravel 中列出本月或每个月的所有日期

html - chrome 中的问题,div 中的 HTML 表单

python - 无法为 Python 2.7.11 安装 openssl-devel

python - opencv-python 和 opencv-contrib-python 的区别

html - 如何让div的高度尽量缩小?

html - Bootstrap 导航栏/边栏问题

javascript - 通过 JavaScript 设置的表单字段的值不会作为 $_POST 的一部分传递给 PHP 脚本

javascript - 如何切换/折叠 javascript 元素(我想默认折叠 html)