我的css位于
static/css/boostrap.css
我目前有2个观看次数。登录视图和仪表板视图。
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)
当我加载登录页面时,它会在
中查找静态文件/static/css/login.css
但是当我加载仪表板时,它会在
中查找它dashboad/static/css/bootstrap.css
login.html css参考
<link href="static/css/signin.css" rel="stylesheet">
dashboard.html css参考
<link href="static/css/dashboard.css" rel="stylesheet">
它将URL的一部分添加到静态文件的路径中,并且不能为我的生活找出如何阻止它。
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',
)
答案 0 :(得分:1)
您使用相对网址链接到资源:因此它们始终从当前页面的目录开始。
确保始终使用前导斜杠:
<link href="/static/css/dashboard.css" rel="stylesheet">
更好的是,使用Django的static
标签自动输出STATIC_URL的值,无论它是什么:
{% load static %}
...
<link href="{% static "css/dashboard.css" %}" rel="stylesheet">