Santhosh
Santhosh

Reputation: 93

Render images from media directory Django

I have a model to store images of all users. Each user will have an uploaded image as well a thumbnail image. All the images will be stored in the path '/media/users/<username>/user_image.jpg' Say the user is ABC: directory structure will be: /media/users/ABC/ABC_image.jpg and /media/users/ABC/thumbnail/ABC_image.img

I could get the path of the image from the ImageField of this particular User model. I have a template where I want to display this image. How can I achieve this?

Upvotes: 1

Views: 1017

Answers (1)

user8123592
user8123592

Reputation:

to display the media file, you need to configure your settings.py as,

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

and in your project's urls.py

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

in your template,

<img src="{{your_model_instance.image_field.url}}"/>

Upvotes: 2

Related Questions