【问题标题】:CSS file won't load - DjangoCSS文件不会加载 - Django
【发布时间】:2020-09-26 03:36:11
【问题描述】:

我在加载 CSS 文件时遇到问题。我使用的浏览器是 Chrome。 这是我的代码和文件目录。 谢谢!

file directories

• 电子商务/电子商务/settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
....

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'store.apps.StoreConfig',
]

....

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'


STATICFILES_DIRS = [
  os.path.join(BASE_DIR, 'static/')
]

• 电子商务/电子商务/urls.py

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('store.urls'))
]

• 电子商务/电子商务/静态/css/main.css

body{
    background-color:blue;
}

• 电子商务/电子商务/商店/模板/商店/商店.html

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">

<h3>store</h3>

<img src="{% static 'images/cart.png' %}">

• 电子商务/电子商务/商店/urls.py

from django.urls import path

from . import views

urlpatterns = [
        #Leave as empty string for base url
    path('', views.store, name="store"),
    path('cart/', views.cart, name="cart"),
    path('checkout/', views.checkout, name="checkout"),

]

• 电子商务/电子商务/商店/views.py

from django.shortcuts import render

# Create your views here.
def store(request):
     context = {}
     return render(request, 'store/store.html', context)

def cart(request):
     context = {}
     return render(request, 'store/cart.html', context)

def checkout(request):
      context = {}
      return render(request, 'store/checkout.html', context)

【问题讨论】:

  • 你做了py manage.py collectstatic吗?是只有css还是静态图片也不显示?
  • 是的,我收到一个错误 - “django.core.exceptions.ImproperlyConfigured:您使用的是静态文件应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径。”
  • 什么错误??
  • "django.core.exceptions.ImproperlyConfigured:您使用的是 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径。"
  • 好吧……答案一定能解决你的问题。

标签: django python-3.x


【解决方案1】:

让我们分解一下:您正在开发中,对吧? (至少看起来你是因为你有DEBUG = True)。好吧,在开发过程中,Django 自己提供静态文件。他通过在每个应用程序目录中查找名为 static 的文件夹来做到这一点。 (这是default behaviour

在您的项目结构中,如您随问题附上的图片所示,store 应用程序内没有 static 目录。

你应该有这样的东西:

store/
|   templates/
|   |   (... all your templates )
|   static/
|   |   store/
|   |   |   css/
|   |   |   |   main.css
|   |   |   images/
|   |   |   |   cart.png
|   (... all your other .py files )

请注意:在您附加的同一张图片中,我可以看到您将 css 文件命名为“mian.css”而不是“main.css”

现在,正如this marvelous answer 中所说的那样,您无需在开发中设置STATIC_ROOTSTATICFILES_DIRS 设置,因为您不会在那里使用collectstatic,因为manage.py将处理有关提供静态文件的所有事情。所以,再说一遍:不要在开发中使用collectstatic。没必要。

最后,您需要正确引用模板中的文件。你可以这样做:

{% static 'store/css/main.css' %}

{% static 'store/images/cart.png' %}

分别。

【讨论】:

  • 我遇到了另一个错误 - django.core.management.base.SystemCheckError: SystemCheckError: 系统检查发现了一些问题:错误: ?: (staticfiles.E002) STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置。
  • 这是因为STATIC_ROOTSTATICFILES_DIRS 中的条目都指向同一个目录。 STATICFILES_DIRS 用于为 collectstatic 包含其他目录,因此如果您实际上不需要该设置,只需删除它即可。
  • 我进行了更改,但仍然无法加载 CSS 文件。事实上,我在 Chrome 中打开了开发人员工具,它显示“加载资源失败:服务器响应状态为 404(未找到)”
  • 天哪,它有效!!!!先生,您是英雄!非常感谢。问你一个问题,你过去做过什么项目我可以看看吗?我对 Django 还是很陌生,真的很想了解更多。如果您有其他建议或建议,请告诉我。再次感谢!
【解决方案2】:

在您的 URLs.py 中,

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url(r'^$', include('frontend.urls')),
);


urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

确保您的 settings.py 文件已定义这些内容。

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

STATIC_URL = '/static/'

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

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)

另外,在您的模板中添加顶部:

{%load staticfiles%}

【讨论】:

  • ImportError: cannot import name 'url' from 'django.urls' (/Users/nnakamura/Desktop/ecommerce/virtual/lib/python3.8/site-packages/django/urls/__init__. py)
  • 我按照你说的做了,但它总是给我错误
猜你喜欢
  • 2017-10-08
  • 2020-07-06
  • 2014-12-30
  • 2020-02-01
  • 2019-01-16
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多