[英]STATIC_URL Not Working on Django 1.5
I cannot load .css and .js files. 我无法加载.css和.js文件。 This is what I get on the server as output:
这是我在服务器上得到的输出:
[29/Oct/2013 11:33:26] "GET /static/srt/css/django-admin-widgets.css HTTP/1.1" 404 1700 [29 / Oct / 2013 11:33:26]“ GET /static/srt/css/django-admin-widgets.css HTTP / 1.1” 404 1700
[29/Oct/2013 11:33:26] "GET /static/srt/js/django-admin.multiselect.js HTTP/1.1" 404 1706 [29 / Oct / 2013 11:33:26]“ GET /static/srt/js/django-admin.multiselect.js HTTP / 1.1” 404 1706
settings.py: settings.py:
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'staticfiles'),
)
urls.py: urls.py:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'srt_project.views.home', name='home'),
# url(r'^srt_project/', include('srt_project.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^accounts/', include('registration.backends.default.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# url(r'^accounts/', include('registration.urls')),
url(r'^srt/', include('srt.urls', app_name='srt')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
site_base.html site_base.html
{% load static from staticfiles%}
<!DOCTYPE html>
<html>
<head>
<title>Status Reporting Tool</title>
<link rel="stylesheet" type="text/css" href="{% static 'srt/css/django-admin-widgets.css' %}" />
<script type="text/javascript" src="{% static 'srt/js/django-admin.multiselect.js' %}"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
jQuery.each($("select[multiple]"), function () {
// "Locations" can be any label you want
SelectFilter.init(this.id, "Viewers", 0, "/static/srt/");
});
</script>
Directory Structure: 目录结构:
.---srt_project
| manage.py
|
+---srt
| | admin.py
| | forms.py
| | models.py
| | tables.py
| | tests.py
| | urls.py
| | views.py
| | __init__.py
|
\---srt_project
| settings.py
| srt_project.sqlite
| urls.py
| wsgi.py
| __init__.py
|
\---static
\---srt
+---css
| django-admin-widgets.css
|
\---js
django-admin.multiselect.js
Here is a list of things to check: 以下是要检查的事项列表:
django.contrib.staticfiles
in INSTALLED_APPS
? INSTALLED_APPS
包含django.contrib.staticfiles
? srt
in INSTALLED_APPS
? INSTALLED_APPS
包含了srt
? DEBUG = True
or False
? DEBUG = True
或False
吗? If it's False
then staticfiles_urlpatterns
won't work. False
则staticfiles_urlpatterns
将不起作用。 python manage.py collectstatic
? python manage.py collectstatic
? OK I'm getting a 304 on the .js and .css files, so looks like I have fixed it. 好的,我在.js和.css文件上得到304,所以看来我已经修复了它。 I just had to change my folder name from 'static' to 'staticfiles', in my inner srt_project directory.
我只需要在内部srt_project目录中将文件夹名称从“ static”更改为“ staticfiles”。 Though it doesn't makes any sense to me as to how this is possible, if 'static' is now 'staticfiles':
尽管这对我如何实现没有任何意义,但是如果现在“ static”是“ staticfiles”:
[29/Oct/2013 12:50:48] "GET /static/srt/css/django-admin-widgets.css HTTP/1.1" 304 0
[29/Oct/2013 12:50:48] "GET /static/srt/js/django-admin.multiselect.js HTTP/1.1" 304 0
Could someone please explain it? 有人可以解释一下吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.