3

I have my css located in

static/css/boostrap.css

I currently have 2 views. A login view and a dashboard view.

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'logins.views.login', name='login'),
    url(r'^dashboard', 'dashboards.views.dashboard', name='dashboard'),

    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

When i load up the login page it looks for the static files in

/static/css/login.css

But when i load the dashboard it looks for it in

dashboad/static/css/bootstrap.css

login.html css reference

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

dashboard.html css reference

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

Its adding part of the URL to the path off the static files and can not for the life of me figure out how to stop it.

settings.py

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    '/Users/chrismeek/Documents/Python/virtual/src/static/templates',
)

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/static-only'
    MEDIA_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/media'
    STATICFILES_DIRS = (
        '/Users/chrismeek/Documents/Python/virtual/src/static/static',
    )
2
  • You haven't shown how you're referencing the css in the login and dashboard templates. Commented Aug 30, 2014 at 19:58
  • How do you mean sorry. Very new to this Commented Aug 30, 2014 at 20:07

1 Answer 1

2

You are using relative URLs to link to the assets: so they always start from the current page's directory.

Make sure you always use a leading slash:

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

Even better, use Django's static tag to automatically output the value of STATIC_URL, whatever it happens to be:

{% load static %}
...
<link href="{% static "css/dashboard.css" %}" rel="stylesheet">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.