Uploading images from media folder during runtime.

43 views
Skip to first unread message

Sandip Nath

unread,
Mar 21, 2020, 2:51:10 PM3/21/20
to Django users
I am developing a car rental app. I have esigned it such a way that the car and driver images will be uploaded from admin page during runtime from media folder. The models.py working fine. Images can be uploaded to the media folder. But the images are not getting diplayed on the car page. Should the img source in car template be changed to the path of the media folder?
Please advice.

Juan J. Moreno Piña

unread,
Mar 21, 2020, 5:46:52 PM3/21/20
to django...@googlegroups.com
Please follow these steps:

Display Image in a Django Template (using ImageField)
* Add this to your settings file: ...
* Create a folder named “media” under your project root directory,
meaning the folder will be on the same level as your apps.
* Add these to your main urls.py. ...
* In your model, use ImageField for the field being used for the image. ...
* Add below to your template to load your images.

Yours
JJM
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/deafdbcf-5b91-436d-a8aa-68d58ab80df3%40googlegroups.com.
>


--

*M.Sc. Juan J. Moreno*
En Venezuela (Maracaibo)
Skype:studiom2j
Tel (Italy): +39 02320628565
NB: Telf. mobil (en reparacion)

<https://www.bitrix24.net/id14591859/>

Sandip Nath

unread,
Mar 22, 2020, 1:18:51 AM3/22/20
to Django users
Sir thank you for your prompt reply. But it isn't clear to me. The images of drivers is getting stored in media/driver_photos and cars in media/car_photos, when uploaded from admin page. But car.html and driver.html both cannot display those images. Error occurring at the img src. I have written it as <img src="media/car_photos/{{ image_name.jpg }} alt="{{ image_name }}>. Please advice. Thanks in advance.

mmk mmk

unread,
Mar 22, 2020, 4:04:03 AM3/22/20
to django...@googlegroups.com
you must use load static tag in your templates as follow:
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
i hope this solve the problem for more info refer to django official docs https://docs.djangoproject.com/en/3.0/howto/static-files/
regards


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Sandip Nath

unread,
Mar 22, 2020, 9:59:21 AM3/22/20
to Django users
MMK, That is the solution for static files, not the dynamic one. How to upload images dynamically during runtime, such as the user uploads his/her profile image.


On Sunday, 22 March 2020 00:21:10 UTC+5:30, Sandip Nath wrote:
Reply all
Reply to author
Forward
0 new messages