简体   繁体   English

在 Django 的网络服务器上提供静态文件

[英]Serving static files on a webserver in Django

I am setting up my site on an external server (AWS).我正在外部服务器 (AWS) 上设置我的站点。 Everything seems to be working except for the static files (CSS and images).除了静态文件(CSS 和图像)之外,一切似乎都在工作。

My project is set up like so --我的项目是这样设置的——

/var/www/
     djangoapps
         myproject
             settings.py, apps, etc.    
     djangotemplates
         myproject
             HTML files
/var/www/html
     media
         static    
             Images & CSS files

In the httpd conf file I have --在 httpd conf 文件中,我有——

<Location /mysite>
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE mysite.settings
        SetEnv PYTHON_EGG_CACHE "/var/cache/www/pythoneggs"
        PythonDebug Off
        PythonPath "['/var/www/djangoapps'] + sys.path"
</Location>

<Location "/media/">
       SetHandler None
</Location>

And in settings.py --在 settings.py 中——

STATIC_URL = '/myproject/static/'
STATICFILES_DIRS = ('/var/www/html/media/static/',)

When I load the page, the templates are working, and the URL to the image files is 'correct' (eg, background: url("/myproject/static/email.jpg") . However, the images are not loading. What do I need to change so that the images and CSS will load properly?当我加载页面时,模板正在工作,图像文件的 URL 是“正确的”(例如, background: url("/myproject/static/email.jpg") 。但是,图像没有加载。什么我需要更改图像和 CSS 才能正确加载吗?

Have you seen the official docs on serving static media with mod_python ?您是否看过有关使用 mod_python 提供静态媒体官方文档 I suspect you need a similar SetHandler for your static files that you do for your media files.我怀疑您需要一个类似的 SetHandler 来处理您为媒体文件所做的静态文件。

Also, mod_python is deprecated.此外,不推荐使用 mod_python。

You need to add an alias for your static files to be served from.您需要为要从中提供服务的静态文件添加别名。 Something like:就像是:

Alias /myproject/static/ /var/www/djangoapps/myproject/media/static

This way Apache will serve the static files instead of asking django to handle it.这样 Apache 将提供静态文件而不是要求 django 来处理它。

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

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