I need to display an image in a Django form. My Django form is quite simple - a single text input field. When I initialise my Django form in a view, I 开发者_Python百科would like to pass the image path as a parameter, and the form when rendered in the template displays the image. Is is possible with Django forms or would i have to display the image separately?
Thanks.
If the image is related to the form itself, not to any field in particular, then you can make a custom form class and override one of as_table(), as_ul(), as_p() methods. Or you can just use a custom template instead of leaving the form to render itself. If it is field related then a custom widget is appropriate, as Pierre suggested.
This template responsibility to display this image in your case, I believe. If you form need to be able to send image related information (path, url..), you'll need to create a dedicated widget.
精彩评论