Django不渲染CSS [英] Django not rendering CSS
问题描述
我是编程新手,目前Django无法渲染CSS。这是html请求
I am new to programming and currently Django will not render my CSS. This is the html request
GET http://127.0.0.1:8000/static/blog/main.css net::ERR_ABORTED 404 (Not Found)
这是我当前的设置以及我知道使用static所需的设置文件。我不明白我的路径有错吗?
Here's my current set up and what I know to be required for using static files. I don't understand how my paths are wrong if they are?
我当前的DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
在我的settings.py ...中已安装。
In my settings.py...it is installed.
/Users/jason/PycharmProjects/blog/mysite/mysite/settings.py
/Users/jason/PycharmProjects/blog/mysite/mysite/settings.py
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
以及已定义的网址
STATIC_URL = '/static/'
这是我的CSS文件的文件路径
This is my filepath for my css file
/Users/jason/PycharmProjects/blog/mysite/blog/static/blog/main
这是我的html标头
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'blog/main.css' %}">
我的项目网址
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
我的应用网址
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
推荐答案
我想在@MohitC添加的答案中添加一些内容,所有django初学者都面临的问题, STATIC
s之间有什么区别,我的意思是,它们是3
I'd like to add something to the answer added by @MohitC, Something that all django beginners face, What's the difference between STATIC
s , I mean, They are 3
-
STATIC_URL
:这是提供静态文件的网址 -
STATICFILES_DIRS
:这是所有在开发过程中放置静态文件的地方。 -
STATIC_ROOT
:这是部署时, STATICFILES_DIRS 中 DIRS 的所有内容将被添加。部署网站时,我们将其称为收集静态,它会遍历所有 STATICFILES_DIRS 并将内容复制到 STATIC_ROOT ,以便服务器只有一个文件夹来提供静态内容,此文件夹已映射到要转到STATIC_URL
的请求,在本例中为/ static /
。
STATIC_URL
: This is the url to serve static filesSTATICFILES_DIRS
: This is ALL the places where you'll put static files during development. This has nothing to do with the server setup, This is a django thing.STATIC_ROOT
: This is a place where ALL the contents of your DIRS in STATICFILES_DIRS will be added when you deploy. When we deploy a website, We call collect static and it loops through all the STATICFILES_DIRS and copy the content to STATIC_ROOT so that the server has only one folder for serving static content and this folder is mapped to the requests that are going toSTATIC_URL
which is/static/
in our case.
基本上,您缺少 STATICFILES_DIRS
。
我也注意到了一些事情,我认为您正在关注 Corey Schafer ,该系列很棒,但是我有一个注释,您不需要像 blog.app.BlogConfig
,您只需键入应用程序名称 blog
即可,它可以按预期运行,更轻松& ;更具可读性。
There's something I noticed too, I think you're following Corey Schafer, The series is great but I have a note, You don't need to include the full app path like blog.app.BlogConfig
, You can just type in the app name blog
and it will work as expected, It's easier & More readable.
我想说明另一件事,如果您是学生,请使用PyCham。它将使您的生活更轻松,它将为您提供所有建议,从应用程序名称到字符串路径,甚至任何其他内容。
I'd like to note another thing, If you're a student, Use PyCham. It will make your life easier, It will suggest everything, From app names to string paths to whatever.
这篇关于Django不渲染CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!