[英]Media files not loading in email in Django
I have a Django project deployed on Apache web server. 我在Apache Web服务器上部署了一个Django项目。 I am serving my media files in folder /var/www/media/
. 我在文件夹/var/www/media/
提供我的媒体文件。 My media files load okay when I am on my web application. 当我在我的Web应用程序上时,我的媒体文件加载正常。
I have a feature which sends email to different people. 我有一个功能,可以向不同的人发送电子邮件。 In that email those media files should load, but they show 404 error in console. 在那封电子邮件中,那些媒体文件应该加载,但它们在控制台中显示404错误。
This is what I added in my apache2.conf file: 这是我在apache2.conf文件中添加的内容:
WSGIScriptAlias / /home/search/egeirn/project/egeirn/wsgi.py
WSGIPythonPath /home/search/egeirn/project:/home/search/virtualenvs/egerin/lib/python2.7/site-packages
Alias /media/ /var/www/media/
Alias /static/ /var/www/static/
<Directory /var/www/static>
Require all granted
</Directory>
<Directory /var/www/media>
Require all granted
</Directory>
<Directory /home/search/egeirn/project/egeirn>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
This is my base.py file: 这是我的base.py文件:
...
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/www/media/'
...
This is my production.py file which is loaded in manage.py and wsgi.py : 这是我的production.py文件,它在manage.py和wsgi.py中加载:
from .base import *
DEBUG = False
ALLOWED_HOSTS = ['iro.egerin.com']
MEDIA_URL = 'http://iro.egerin.com/media/'
Please tell what I should do to load my media files out of the application like in email? 请告诉我应该怎样做才能将我的媒体文件从电子邮件中加载到应用程序中?
Just to give an idea this is the error in the console: 只是想知道这是控制台中的错误:
You have missed to add your server name in the template where you have added the media files. 您错过了在添加了媒体文件的模板中添加服务器名称。 You image element should be something like: 你的图像元素应该是这样的:
<img src="{{MEDIA_URL}}{{path_of_image}}" />
You have missed the media_url in the template. 您错过了模板中的media_url。
Make sure you have added the media urls in your project urls.py 确保您已在项目urls.py中添加了媒体网址
urlpatterns += patterns('',url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),)
set the MEDIA_ROOT in the project settings.py , then directly you can access the Images in the template using .url
( {{instace.prodimage.url }}
) 在项目settings.py中设置MEDIA_ROOT,然后直接使用.url
( {{instace.prodimage.url }}
)访问模板中的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.