Django Media图像文件未在Apache的生产环境中加载

时间:2019-07-05 15:01:09

标签: python django apache

用户上传图像后,它没有显示在列表页面上,但是保存在我的作品的媒体文件夹中,但是可以正常工作进行开发。

我在settings.py和urls.py中配置了MEDIA_URL,MEDIA_ROOT,并且还在apache中设置了别名,但仍然没有加载图片

在settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]


MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

在urls.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

在Apache中(/etc/apache2/sites-available/000-default.conf)

<VirtualHost *:80>

    <Directory /var/www/html/ourdemocracy/newproject/newproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess newproject python-path=/var/www/html/ourdemocracy/newproject python-home=/var/www/html/ourdemocracy/venv
    WSGIProcessGroup newproject
    WSGIScriptAlias / /var/www/html/ourdemocracy/newproject/newproject/wsgi.py

    Alias /static /var/www/html/ourdemocracy/newproject/static
    <Directory /var/www/html/ourdemocracy/newproject/static>
        Require all granted
    </Directory>

    Alias /media /var/www/html/ourdemocracy/newproject/media/>
    <Directory /var/www/html/ourdemocracy/newproject/media/>
        Require all granted
    </Directory>

</VirtualHost>

在列表页面上

{% if campaign.campaign_image %}
    <img src="{{ MEDIA_URL }} {{ campaign.campaign_image.url }}" alt="{{ campaign.title }}">
 {% else %}
     <img src="{% static "img/cause-6.jpg" %}" alt="">
{%endif%}

当我定位媒体映像时,它显示以下错误 在此服务器上找不到请求的URL /media/campaigns/images/myimage.jpg。

0 个答案:

没有答案