CSS文件将无法加载-Django

问题描述 投票:0回答:2

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

file directories

•ecommerce / ecommerce / 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/')
]

•ecommerce / ecommerce / 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;
}

•ecommerce / ecommerce / store / templates / store / store.html

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

<h3>store</h3>

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

•ecommerce / ecommerce / store / 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"),

]

•ecommerce / ecommerce / store / 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)
django python-3.x
2个回答
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”]

[现在,正如[C​​0]中所说的那样,您无需在开发中设置this marvelous answerSTATIC_ROOT设置,因为您不会在那里使用STATICFILES_DIRS,因为collectstatic将处理有关提供静态文件的所有事宜。因此,再次:不要在开发中使用manage.py。不需要。

最后,您将需要正确引用模板中的文件。您可以按如下操作:

collectstatic

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

分别。


1
投票

在您的URLs.py中,

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

请确保您的settings.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)

此外,在模板的顶部添加:

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',
)
© www.soinside.com 2019 - 2024. All rights reserved.