Liondancer
Liondancer

Reputation: 16489

Loading static files in template

I am pretty new to Django and I am trying to put static files in my site but I am unable to do so. I am getting 404 when the browser tries to GET my static files

I'm not sure of the best practices or the correct way for Django to find these static files

Here is a picture of my project structure:

enter image description here

Here is my settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

In my index.html I have a line:

<link href="{{ STATIC_URL }}boothie.css" rel="stylesheet">
<script src="{{ STATIC_URL }}boothie.js"></script>
<script src="{{ STATIC_URL }}jquery.js.1.3.js"></script>

Upvotes: 0

Views: 215

Answers (2)

dazedconfused
dazedconfused

Reputation: 1342

I think this would work:

{% load staticfiles %}

<link href="{% static "css/boothie.css" %}" rel="stylesheet">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.js.1.3.js" %}"></script>

See: Configuring static files

Edit:

You can maintain static files which belong to your home app with the structure like this:

home/
    static/
        css/
        images/
        js/

While deploying, you can set STATIC_ROOT to where you want to serve these static files, then run python manage.py collectstatic, django will copy your static files into the STATIC_ROOT directory and you can maintain static files easier (in a single directory) without changing your code.

Upvotes: 1

Raja Simon
Raja Simon

Reputation: 10315

static_url will give the relative path ....

You may have to include

<link href="{% static "css/boothie.css" %}" rel="stylesheet">

Upvotes: 1

Related Questions