I have a Django project deployed on Apache web server. I am serving my media files in folder /var/www/media/
. My media files load okay when I am on my web application.
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.
This is what I added in my apache2.conf file:
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:
...
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 :
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.
Make sure you have added the media urls in your project 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 }}
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.