Search code examples
pythondjangostandardstwitter-bootstrapconventions

How to handle assets (css, js, etc) with Django?


I'm new to Django and to Python, and I've just completed Django's "build your first app" tutorial. I found it fantastic. However it never spoke about stylesheets, javascripts or anything of the like.

Where should they go? Currently my project directory looks like this:

myproject
  +app1
  +app2
  +myproject
  +public
    +css
    +img
    +js
  +templates

Am I doing it correctly? I would like to stick to Django/Python standards as much as possible. Also, if I would like to include certain styles and scripts in all of my templates, how can I do that? Let's say I'm using Bootstrap.


Solution

  • You should probably take a closer look at Managing static files.

    Basically you can put a static folder in each of your apps (similar to your template folders) and the more general stuff (base.css, project-wide icons, jquery-plugins) in a static folder in your project root.

    So the layout might look like this:

    myproject
      +app1
        +static
          +app1
            +css
            +js
      +app2
      +static
        +css
        +img
    

    Once you deploy your project you can use, the collectstatic-command from contrib.staticfiles to collect all your static files to a single directory (STATIC_ROOT) where you can serve it from.