繁体   English   中英

Django无法在我的开发机上识别我的静态文件

[英]Django won't recognize my static files on my dev machine

我的任务是修复django 1.3站点的某些方面,该站点在服务器上的Apache / PostgreSql上运行。 我正在尝试在具有postgres和内部python开发人员服务器的虚拟环境的开发计算机上进行设置。

我设法使站点开始运行并从本地pg实例读取信息,但是无法识别存储在/site_media静态文件。 该站点将很快被重写为使用django 1.6和正确的/static文件夹,但是现在我需要修复此站点。

我也尝试使用nginx和gnunicorn运行它,但结果是相同的,该站点显示但没有样式,并且对静态目录中文件的所有引用都给出了404。进一步检查404显示django正在尝试解析资源与它的路由器。

以下是相关设置:

settings.py:

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/nico/src/df2/datos/site_media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://localhost:8000/site_media/'

我还添加了以下配置,但无济于事:

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

STATICFILES_DIRS = (
    '/home/nico/src/df2/datos/site_media/',
)

STATIC_URL = 'http://localhost:8000/site_media'

Nginx配置文件:

server {
    server_name localhost;

    access_log off;

    location /site_media/ {
        alias /home/nico/src/df2/datos/site_media;
    }

    location / {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

如果您需要任何其他配置,请告诉我。

我宁愿仅从python服务器运行此程序,但使用gunicorn / nginx的解决方案也可以

解决方法是在urlpatterns变量中添加静态处理程序:

from django.conf import settings

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM