Reputation: 51
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'),
Upvotes: 0
Views: 158
Reputation: 2110
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
: 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 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.
Upvotes: 1
Reputation: 4781
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"),
)
Upvotes: 0