Django在使用开发服务器时不会提供静态文件 [英] Django won't serve static files while using development server

查看:61
本文介绍了Django在使用开发服务器时不会提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为正在工作的网站启动了一个新的开发服务器,但似乎无法让Django开发服务器来提供用于CSS和其他功能的静态文件。管理站点的CSS加载正常。我正在virtualenv沙箱中运行它。

I just started a new development server for a website I am working on and I can't seem to get the Django development server to serve the static files I have for CSS and other things. The CSS for the admin site loads fine. I am running it in a virtualenv sandbox.

在settings.py中,我搞砸了MEDIA_ROOT和MEDIA_URL。

In settings.py I've messed around with MEDIA_ROOT and MEDIA_URL.

到目前为止,我已经尝试过MEDIA_ROOT。

So far for MEDIA_ROOT I've tried.

MEDIA_ROOT = '/home/wluw/wluw/wluw/media'

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')

我将ADMIN_MEDIA_PREFIX更改为

I changed my ADMIN_MEDIA_PREFIX to

ADMIN_MEDIA_PREFIX = '/admin_media/'

我的MEDIA_URL看起来像这样

my MEDIA_URL looks like this

MEDIA_URL = '/media/'

和网址。静态文件的py部分如下所示。

and the urls.py section for the static files looks like this.

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

这是当我尝试访问页面时来自开发服务器的输出。

Here's the output from the dev server when I try to access the page.

[21/Jul/2011 21:19:25] "GET /media/css/style.css HTTP/1.1" 302 0
[21/Jul/2011 21:19:25] "GET /media/css/style.css/ HTTP/1.1" 404 2561



from django.conf.urls.defaults import patterns, include, handler500, handler404
from django.conf import settings
from django.contrib import admin
import d51_django_admin_piston

handler500 = 'radio.frontend.views.server_error'

admin.autodiscover()
d51_django_admin_piston.autodiscover(admin.site)

urlpatterns = patterns(
'',
(r'^logs/', include('radio.logs.urls')),
(r'^events/', include('radio.events.urls')),
(r'^station/', include('radio.station.urls')),
(r'^staff/', include('radio.staff.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
(r'^', include('radio.frontend.urls')),
)

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

这是我的radio.frontend.urls

Here is my radio.frontend.urls

from django.conf.urls.defaults import *


urlpatterns = patterns('radio.frontend.views',
    url(r'^$', 'home', name='home'),
)

这是我的设置。py
settings.py

在具有/ media的生产服务器上,一切正常?

Everything was working fine on the production server having /media? being the url for css and other things.

也不会显示数据库中的任何内容。该网站的每个页面均使用base.html和viewname.html创建。仅显示base.html部分。我确信这是另一个问题的话题。

Also none of the content in the database is being shown. Each page of the site is created with a base.html and a viewname.html. Only the base.html part is showing up. I am sure this is a topic for an other question though.

我看过很多其他帖子,其中有同样的问题,但没有一个提供解。

I've looked at a ton of other posts with people having the same problem and none of them provided a solution. I am completely stumped.

任何帮助将不胜感激。谢谢

Any help would be greatly appreciated. Thanks

推荐答案

您正试图通过'/ static /'而不是'/ media /'访问静态文件,因为在您的您说的评论:

You are trying to access your static files via '/static/' instead of '/media/' since in your comments you say:

"GET /static/css/style.css HTTP/1.1" 302 0 [21/Jul/2011 21:13:31] "GET /static/css/style.css/ HTTP/1.1" 404

您可以通过这种方式访问​​它:

Either you access it that way:

"GET /media/css/style.css HTTP/1.1" 302 0 [21/Jul/2011 21:13:31] "GET /media/css/style.css/ HTTP/1.1" 404

然后在模板中相应地设置URL。

And you set you URL in your templates accordingly.

或者,您可以通过以下方式设置路由:

Or, you setup your routing this way:

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

我选择第二个,然后设置p settings.STATIC_FILE ,因为您通常使用 MEDIA_ROOT 来上传/下载内容。

I'll choose the second one and would setup settings.STATIC_FILE as you usually use MEDIA_ROOT for upload/download content.

这篇关于Django在使用开发服务器时不会提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆