Pregunta

I have my static files in a folder assets in the application directory. When I go to the main page (/), the static files are being loaded perfectly fine from /assets/. If I go to /house/, it tries to load the static files from /house/assets/, which obviously results in the files not loading as they are not there.

This is the possibly relevant piece of settings.py:

...
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
STATIC_PATH = os.path.join(BASE_DIR, 'assets')
STATIC_URL = os.path.join(BASE_DIR,'/assets/')

STATICFILES_DIRS = (
    STATIC_PATH,
)
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)
...

I am loading the static files in the templates using something like this:

<link href="assets/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>

This is the urls file for that part of the application:

urlpatterns = patterns('',
    url(r'^$', views.dashboard, name='dashboard'),
    url(r'^house/$', views.house, name='house'),
)

and these are the views:

def dashboard(request):
    return render_to_response('index.html')

def house(request):
    return render_to_response('house.html')

I have been looking for a solution to this for the past hour with no success. I have found this post asking a similar thing but it did not help. Any help will be greatly appreciated.

¿Fue útil?

Solución

You should change the HTML tag in your template from

<link href="assets/plugins/uniform/css/uniform.default.css"
      rel="stylesheet" type="text/css"/>

to

<link href="/assets/plugins/uniform/css/uniform.default.css"
      rel="stylesheet" type="text/css"/>

Please note the slash / in front of the relative URL. Without it, the browser will assume that assets directory is a subdirectory of the current one. With it, it will always start from the root directory.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top