【发布时间】:2020-09-26 03:36:11
【问题描述】:
我在加载 CSS 文件时遇到问题。我使用的浏览器是 Chrome。 这是我的代码和文件目录。 谢谢!
• 电子商务/电子商务/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