本文介绍了Django静态文件-无法加载CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Django,并且正在尝试加载静态CSS文件.我看过其他问题并阅读了文档,但是仍然看不到问题.我正在使用Django 1.11.

I am learning Django, and am trying to load a static css file.I have seen the other questions and read the docs, but I am still unable to see the problem.I am using Django 1.11.

这是我的urls.py:

Here is my urls.py:

from django.conf.urls import url
from django.contrib import admin

from django.conf import settings
from django.conf.urls.static import static

from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我的settings.py(仅与静态文件有关的部分):

My settings.py (only the part to do with static files):

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

STATICFILES_DIRS = (
    STATIC_ROOT,
)

以及我尝试加载文件的模板部分:{%静态负载%}

And the part of my template where I try and load the files: {% load static %}

每当我加载index.html模板时,控制台都会显示以下错误消息:

Whenever I load the index.html template, the following error messages are displayed by my console:

core.js:5 Uncaught ReferenceError: define is not defined
    at core.js:5
localhost/:12 GET http://localhost:8000/static/css/homepage.css
localhost/:11 GET http://localhost:8000/static/css/horz-navbar.css
localhost/:10 GET http://localhost:8000/static/css/fonts.css
localhost/:13 GET http://localhost:8000/static/css/style.css

这是文件目录结构:

mysite
  db.sqlite3
  manage.py
  mysite
    __init__.py
    settings.py
    urls.py
    views.py
    wsgi.py
  static
    admin
    css
      fonts.css
      horz-navbar.css
      homepage.css
      style.css
  templates
    index.html

因此Django似乎无法识别这些文件是否存在,我已经检查并确保文件在我的计算机上存在,并且不确定是否需要这些文件,但是我还运行了 pythonmanage.py collectstatic

So Django doesn't seem to recognize that the files exist, I have checked and made sure that the files exist on my computer, and I'm not sure if this was needed, but I have also run python manage.py collectstatic

请告诉我是否需要更多信息.

Please tell me if you need anymore information.

推荐答案

将您的 STATIC_ROOT 更改为另一个名称,然后更新您的 STATICFILES_DIR .像这样:

Change your STATIC_ROOT into another name and then update your STATICFILES_DIR. Something like this:

STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

这篇关于Django静态文件-无法加载CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:19