无法加载静态文件
Unable to load static files
我在 settings.py
中添加了以下内容:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
在 urls.py
中:
urlpatterns = patterns('^/static/$',
# ... the rest of your URLconf goes here ...
url(r'^$', home, name='home'),
url(r'^admin/', include(admin.site.urls))
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在 models.py
中:
class Search(models.Model):
question = models.URLField()
query = models.CharField(max_length=255)
在 views.py
中:
def home(request):
print ' base dir %s' % BASE_DIR
print ' static dir %s' % STATIC_URL
form = SearchForm()
return render_to_response('base.html', {'form': form},
context_instance=RequestContext(request))
和templates/base.html
:
{% load staticfiles %}
<head>
<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="{{ STATIC_URL }}bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<title>Walker</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group">
<form name="query">
{{ form.as_p }}
</form>
</div>
</div>
</div>
</div>
</body>
我 运行 ./manage.py collectstatic
没有问题,正在创建文件夹 static
。
当我执行 ./manage.py runserver
时,我可以在终端中看到这个错误:
基本目录 home/my/code/walker
静态目录 /static/
"GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 1691
模板加载正确。但是bootstrap不
几个小时都搞不清楚我需要做什么
如果您将静态文件存储在 app/static
以外的位置,我认为您需要在 settings.py
中指定 STATICFILES_DIRS
否则它们将不会被收集:
STATICFILES_DIRS = (
"/path/to/static", #collectstatic will find any app/static directories so do not add those here
)
default为空
此外,使用 href="{% static 'my-static.css' %}"
而不是 {{ STATIC_URL }}
指定静态文件
您还可以使用 findstatic 检查您的静态文件是否能够被服务器定位。
我在 settings.py
中添加了以下内容:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
在 urls.py
中:
urlpatterns = patterns('^/static/$',
# ... the rest of your URLconf goes here ...
url(r'^$', home, name='home'),
url(r'^admin/', include(admin.site.urls))
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在 models.py
中:
class Search(models.Model):
question = models.URLField()
query = models.CharField(max_length=255)
在 views.py
中:
def home(request):
print ' base dir %s' % BASE_DIR
print ' static dir %s' % STATIC_URL
form = SearchForm()
return render_to_response('base.html', {'form': form},
context_instance=RequestContext(request))
和templates/base.html
:
{% load staticfiles %}
<head>
<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="{{ STATIC_URL }}bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<title>Walker</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="form-group">
<form name="query">
{{ form.as_p }}
</form>
</div>
</div>
</div>
</div>
</body>
我 运行 ./manage.py collectstatic
没有问题,正在创建文件夹 static
。
当我执行 ./manage.py runserver
时,我可以在终端中看到这个错误:
基本目录 home/my/code/walker
静态目录 /static/
"GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 1691
模板加载正确。但是bootstrap不
几个小时都搞不清楚我需要做什么
如果您将静态文件存储在 app/static
以外的位置,我认为您需要在 settings.py
中指定 STATICFILES_DIRS
否则它们将不会被收集:
STATICFILES_DIRS = (
"/path/to/static", #collectstatic will find any app/static directories so do not add those here
)
default为空
此外,使用 href="{% static 'my-static.css' %}"
而不是 {{ STATIC_URL }}
您还可以使用 findstatic 检查您的静态文件是否能够被服务器定位。