Django not rendering CSS

不打扰是莪最后的温柔 提交于 2020-05-17 06:22:38

问题


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)

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?

My current DIR

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

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

In my settings.py...it is installed.

/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',

along with URL defined

STATIC_URL = '/static/'

This is my filepath for my css file

/Users/jason/PycharmProjects/blog/mysite/blog/static/blog/main

and this is my html header

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

my project urls

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

my app urls

urlpatterns = [
    path('', views.home, name='blog-home'),
    path('about/', views.about, name='blog-about'),

回答1:


I'd like to add something to the answer added by @MohitC, Something that all django beginners face, What's the difference between STATICs , I mean, They are 3

  • STATIC_URL: This is the url to serve static files
  • STATICFILES_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 to STATIC_URL which is /static/ in our case.

Basically, You are missing STATICFILES_DIRS.

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.

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.




回答2:


You need to add STATICFILES_DIRS in your settings.py which should point to your staic folder. It is usually done as

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)


来源:https://stackoverflow.com/questions/61433863/django-not-rendering-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!