Css wont load

Hi for some reason my CSS file won’t load.
I try to search the internet but didn’t found a solution.
can you assist me ?

my settings.py
STATIC_ROOT= os.path.join(BASE_DIR,‘staticfiles’)
STATIC_URL = ‘/static/’
MEDIA_ROOT = os.path.join(BASE_DIR, ‘media/’)
MEDIA_URL = ‘/media/’
STATICFILES_DIRS = (
os.path.join(BASE_DIR, ‘static’),
)

my html page:
in the start i have :
{% load static %}

   <script src="{% static 'js/main.js' %}"></script>
  <link rel=”stylesheet” href=" {% static 'css/main.css' %} ">

Have you run python manage.py collectstatic ?

Also, Django won’t serve static when in production. If you need to serve static files, then you will need to either host them somewhere like AWS or your own webserver, or use a third party package like whitenoise. Check it out here: https://whitenoise.evans.io/en/stable/django.html

Lastly, you will need to give Django a URL from which to serve static files, for example,

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This is taken from the docs here: https://docs.djangoproject.com/en/3.0/howto/static-files/

I would strongly recommend that you give that a read. Good luck!

1 Like

thank you very much, i will try that.

I have this same problem. Admin area doesn’t load css style. Also, my app does not load css styles.
I have read everything and followed all processes available on the internet, but its still not working.

I really need help at this point. I can’t think of any solution.
Please help me resolve this issue.

Please post your settings.py file and urls.py

Thank you for your help.

Here’s the urls.py file. I had to take everything back to default after trying everything possible

Hi, please i still need help to make css load on my page. I have tried everything afresh all over again, but its still not working.

There is not really enough data to diagnose your problem.

It would help to see the output of the Network tab on the Chrome DevTools as the page loads. I suspect that your site is returning 404 errors for the CSS and JavaScript files.

Here are some questions to answer:

  • How are you running Django? Are you using ./manage.py runserver or some other Python webserver?
  • Is DEBUG set to True or False in your settings.py?
  • Did you run ./manage.py collectstatic as recommended earlier in the thread?

Hi, Thank you for your help.

I use python manage.py runserver to run Django.

DEBUG is set to True

Yes, i ran ./manage.py collectstatic and some static contents were created in the static folder, but it still didn’t work.

If there’s something else i should send to you to help with the problem, please let me know, i will really appreciate your help.

Here’s the output on Chrome DevTool

The screenshot you shared is from your actual application rather than the admin. We can see from the network tab that the static assets like boostrap.min.css are loading (and that the page contains some bootstrap styles). Can you share a similar screenshot from the /admin/login/ page where the static assets do not appear to be loading?

Also, can you list what you have in INSTALLED_APPS in your settings file?

By the way, for sharing code on the forum, instead of using a screenshot, you can use the backtick (`) character when you put in code. This is sometimes called “fenced code” because 3 backtick characters are used above and below the code and make something that seems like a fence (note: not apostrophe characters!).

    ```python
    print('hello world')
    ```

This is an example of what that would look like. I had to add extra indentation for this example or else it would come out wrong in the forum. More documentation about this feature is at: https://meta.discourse.org/t/how-do-i-select-a-language-in-code-blocks/19247

Thank you very much for your help mblayman.

I was able to discover the solution.

I added

import mimetypes
mimetypes.add_type("text/css", ".css", True)

to settings.py file, and it solved the problem.

I am really grateful for your support.

2 Likes

Wow, I don’t think I would have guessed that your Python was missing the CSS mimetype, but I’m glad you figured it out! Good luck with your project.

Hi mblayman,

The problem was solved thanks to your first message.

It was part of the output of the Network tab on the Chrome DevTools after i cleared cache and reloaded the page.

I am currently going through https://www.mattlayman.com/ and i already have it and https://www.twitch.tv/mblayman bookmarked. I am a beginner and there’s a lot to learn :slight_smile:

Thank you once more for your help, i am really grateful.

1 Like

@ogbodotg Hello , My project also seem to have the same problem. CSS files dont seem to load.
I tried your fix it dint seem to work for me

@ogbodotg The CSS files seemed to work fine after I cleared my cache and hit reload a few times. Is there a reason for this type of behavior or is there something wrong with the way I am working with static files.

You’re probably not doing anything wrong.

What you can do is use either shift-reload or shift-F5 to force files to be loaded. You can also look at your console output where the requests are being served. If you see a 200 response code that means the files were loaded, but if you get a 304 response, that means that it found the file in cache and is using the cached version.

You could configure your app to disable caching, that can help when you’re doing a lot of work with your CSS.

Hey @KenWhitesell I just observed 304 response, Does tat convey my files being stored in cache?

You are correct, that’s exactly what that means. (Yea, I know I originally posted 302 in my previous reply. Oops)

@KenWhitesell Can you please explain the process of configuring app to disable caching during development.