I'm trying to upload an image via the Django admin and then view that image either in a page on the frontend or just via a URL.
Note this is all on my local machine.
My settings are as follows:
MEDIA_ROOT = '/home/dan/mysite/media/'
MEDIA_URL = '/media/'
I have set the upload_to parameter to 'images' and the file has been correctly uploaded to the directory:
'/home/dan/mysite/media/images/myimage.png'
However, when I try to access the image at the following URL:
http://127.0.0.1:8000/media/images/myimage.png
I get a 404 error.
Do I need to setup specific URLconf patters for uploaded media?
Any advice appreciated.
Thanks.
Here What i did in Django 2.0. Set First MEDIA_ROOT an MEDIA_URL in
setting.py
Then Enable the
media
context_processors
inTEMPLATE_CONTEXT_PROCESSORS
by addingYour
media context processor
is enabled, Now everyRequestContext
will contain a variableMEDIA_URL
.Now you can access this in your
template_name.html
(at least) for Django 1.8:
If you use
as described above, make sure that no "catch all" url pattern, directing to a default view, comes before that in urlpatterns = []. As .append will put the added scheme to the end of the list, it will of course only be tested if no previous url pattern matches. You can avoid that by using something like this where the "catch all" url pattern is added at the very end, independent from the if statement:
This is what I did to achieve image rendering in DEBUG = False mode in Python 3.6 with Django 1.11
For Django 1.9, you need to add the following code as per the documentation :
For more info, you can refer here : https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user-during-development
Adding to Micah Carrick answer for django 1.8:
This if for Django 1.10: