Show image in Django Form

I need to display an image in a Django form. My Django form is pretty simple - one text input field. When I initialize the Django form in the view, I would like to pass the image path as a parameter, and the form displays the image when rendering in the template. Is this possible with Django forms or will I need to display the image separately?

Thank.

+3
source share
2 answers

If the image is associated with the form itself, and not with any specific field, you can create your own form class and override one of as_table () , as_ul () , as_p () . Or you can just use your own template instead of leaving a form for rendering. If this is field related, then the custom widget is suitable, as Pierre suggested.

+1
source

This template is responsible for displaying this image in your case, I believe. If you form, you must be able to send information related to the image (path, url ..), you need to create a dedicated widget.

+1
source

Source: https://habr.com/ru/post/1750643/


All Articles