Django:静态文件

时间:2012-03-23 15:37:49

标签: django django-templates

当我尝试从我的博客中提供图像时,模板正在查找

[26/Mar/2012 10:33:42] "GET /blog/images/coke.jpg HTTP/1.1" 200 6153

然而,当我从平面页面加载图像时,模板会查找

[26 / Mar / 2012 10:33:42]“GET /blog/images/coke.jpg HTTP / 1.1”200 6153

我认为这是因为我需要使用我的博客应用程序向我的urls.py文件中添加一些内容,以便它知道我的静态URL是什么,而不是查看blog / images文件夹。

我正在使用标签:

src="{{ STATIC_URL }}images/header.png"

我的settings.py有:

STATIC_ROOT = '/export/mailgrp4_a/sc10jbr/WWWdev/dbe/djangostatic/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/export/mailgrp4_a/sc10jbr/WWWdev/dbe/static/',

我的博客url.py包含:

urlpatterns = patterns('dbe.blog.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)

我的主要url.py有:

urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)

我需要添加什么?

我更新的博客url.py包含:

from django.conf.urls.defaults import *
from dbe.blog.models import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = patterns('dbe.blog.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)

urlpatterns += staticfiles_urlpatterns()

我更新的主url.py包含:

from django.conf.urls.defaults import *
from dbe.blog.models import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),

)

urlpatterns += staticfiles_urlpatterns()

其他人可以帮忙解决这个问题,即使按照下面的帮助后我也真的陷入困境。有什么我可以用来帮助调试和诊断问题吗?

感谢

3 个答案:

答案 0 :(得分:2)

在开发过程中,您可以将其添加到urls.py.完成后,必须将Web服务器配置为提供静态文件。

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
         url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
   )

答案 1 :(得分:0)

好像你想念以下事情:

settings.py(可能需要syncdb

INSTALLED_APPS = (
...
"django.contrib.staticfiles",
...
)

TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
...
)

main urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

运行collectstatic命令

还有包含所有详细信息的文档页面:

https://docs.djangoproject.com/en/1.3/howto/static-files/

答案 2 :(得分:0)

/blog/images/header.png请求图像的唯一方法是将{{ STATIC_URL }}评估为空字符串。唯一可能发生的方法是,如果它没有在上下文中设置,唯一的方法就是如果你没有包含staticfiles模板上下文处理器:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.static',
)