dl8
dl8

Reputation: 1270

Django, trouble with STATIC_URL file paths

I have two templates index.html and search_results.html in the same directory.

Both have the following code snippet

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />

The css file loads on the index template:

"GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0

However, does not for the search_results template:

"GET /search/static/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 2133

Since it includes the /search.

I'm wondering how to omit the /search/ portion in trying to load the css file in my second template, since that template all have urls starting with /search/.

my urls.py:

urlpatterns = patterns('',
    url(r'^$', 'home.views.index'),
    url(r'^search/$', 'home.views.film_chart_view')

)

Upvotes: 0

Views: 511

Answers (1)

Philipp Zedler
Philipp Zedler

Reputation: 1670

Clearly, you are missing the slash / at the beginning of the static URL.

The dirty fix: Change the line in your template to

<link rel="stylesheet" type="text/css" href="/{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />

The clean fix: Add a slash at the beginning of the STATIC_URL variable in your settings file. It must probably look like this:

STATIC_URL = '/static/'

Upvotes: 2

Related Questions