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.

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