我尝试了以下(接受的答案):Django static files on heroku但似乎没有用。
问题是虽然路径似乎是正确的,但在显示模板html时我无法获得正确的行为。我正在尝试加载jquery库和表分类器。如果我尝试实际位置:www.mydomain.com/static/javascript/jquery.tablesorter.js
它可以正常访问。但是在具有以下内容的模板中,我对{{staticvalues}}
或{{static}}
没有任何价值。
以下是模板:
{% load staticfiles %}
files: {{ staticfiles }}
files: {{ static }}
<script type="text/javascript" src="{% static "/javascript/jquery-1.11.2.min.js" %}"></script>
<script type="text/javascript" src="{% static "/javascript/jquery.tablesorter.js" %}"></script>
<table id="myTable" class="tablesorter">
这是我的urls.py:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
import hello.views
from diy import settings
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'diy.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', hello.views.index, name='index'),
url(r'^db', hello.views.db, name='db'),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'hello.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
settings.py:
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
查看日志文件,我发现以下内容:
2015-01-27T18:12:36.780895+00:00 heroku[router]: at=info method=GET path="/javascript/jquery.tablesorter.js" host=xx.herokuapp.com request_id=47d2671b-7905-46cd-8786-817c063051e9 fwd="xx" dyno=web.1 connect=1ms service=51ms status=404 bytes=2555
2015-01-27T18:12:36.817026+00:00 heroku[router]: at=info method=GET path="/javascript/jquery-1.11.2.min.js" host=xxx.herokuapp.com request_id=a4619d34-f76b-4ceb-970a-ab6008585410 fwd="xx" dyno=web.1 connect=1ms service=67ms status=404 bytes=2552
正如您所期望的那样{{static}}
为空。我尝试对模板进行以下更改,发现问题无法解决,但是我得到状态= 200。我不应该使用{{static}}
作为模板中的静态位置吗?