0

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'),
2
  • Do your files have read permissions ? check that once Commented Apr 26, 2020 at 17:17
  • How do I check that? I haven't done anything with read permissions. Commented Apr 27, 2020 at 14:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yes! I am following Corey's Django tutorial. In his static file section he did not mention anything about setting up a STATIC_ROOT and STATICFILES_DIR...and only showed how to set up the static directories and direct html to it with href. Thanks for the clarifications on the 3 types. That makes alot more sense now...and yes, I'm currently using pycharm!
He'll mention it lately, This is something I didn't understand at the beginning when I watched him, Glad I helped.
You didn't understand at the time but were you able to render your css?
Yea I just followed, Everything gets clearer by time
0

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"),
)

3 Comments

still get GET 127.0.0.1:8000/static/blog/main.css net::ERR_ABORTED 404 (Not Found)
Update your question with your URL's, BASE_DIR, and path where settings.py is located.
Update STATICFILES_DIRS and BASE_DIR code from settings.py as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.