DEV Community

Cover image for Loading styles and static files in Django
Graham Morby
Graham Morby

Posted on

7 5

Loading styles and static files in Django

I like you am a newbie when it comes to Django. Yes I am slightly in love with the Framework and slightly excited about what it can do and how powerful it can be moving forward. But the project I'm working on I needed to be able to load a custom stylesheet in and overwrite some bootstrap bits, now there are plenty of little tutorials out there but they all seem to be for older versions of the framework. So I thought I'd quickly lay out how its being done in the current version:

Create a static folder in the main folder of your project. The one where the manage.py folder is. Inside there add a CSS folder and your style.css stylesheet. Should look like this

  • manage.py
  • myapp
  • static
    • css
    • *style.css
  • templates

So now head over to your base.html and add this to the top of the page

{% load static %}
Enter fullscreen mode Exit fullscreen mode

And then in the

portion add a link to our css
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
Enter fullscreen mode Exit fullscreen mode

And finally in our settings.py file add

# Static file route
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
Enter fullscreen mode Exit fullscreen mode

css loaded

And thats it! We should be all cooking on gas and have the styles loading in, also if you want JS or any other static file to load you can add it all to the static folder, super clean and super easy!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay