Mantis
Mantis

Reputation: 1387

django's class based views static files

I am having some issues loading my css and javascript files when using generic class based views.

The folder structure is like this.

- app1
  - views.py ect
  - templates
    - app1.html
- static
  - static
    - css
    - base.css

app1.html:

<link href="/static/css/base.css" rel="stylesheet">

views.py:

class ClientList(ListView):
    model = Client
    template_name = 'app1.html'

I tried moving the static files inside the app but it still cant find them

If you need anymore info please let me know.

Upvotes: 0

Views: 656

Answers (1)

catavaran
catavaran

Reputation: 45575

You don't need additional static dir inside the root static:

- app1
  - views.py
  - templates
    - app1.html
- static
  - css
    - base.css

So in the template you can refer the base.css as:

{% static 'css/base.css' %}

And don't forget to set the STATICFILES_DIRS setting.

If you want to put static files into the app dir the the layout could be like this:

- app1
  - views.py
  - templates
    - app1.html
  - static
    - css
      - base.css

In this case the STATICFILES_DIRS setting is not required.

Note that if you put static files (or templates) into the app dir then it is a good practice to add a dir with the app's name to prevent filename clash with another app:

- app1
  - views.py
  - templates
    - app1
      - base.html
  - static
    - app1
      - css
        - base.css

And then in the template:

{% static 'app1/css/base.css' %}

Upvotes: 3

Related Questions