DEV Community

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

Posted on

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!

Top comments (0)