django - Display initial form.FileField as img tag in template -


my form class:

class myform(forms.form):     image = forms.imagefield() 

in view (get case) instance form (then pass template ctx):

form = myform(initial={'image':my_model_obj.image}) 

now in template wanto display image as:

<img src="{{form.image.url}}"> 

but doesn't work, src stay blank.

as workaround did in view:

form = myform() form.image_display = my_model_obj.image 

and in template:

<img src="{{form.image_display.url}}"> 

is way it? (i know can pass image context variable meh..)

thank in advance

i've not tested :d found in docs

from django.core.files.uploadedfile import simpleuploadedfile  file_data = {'image': simpleuploadedfile(my_model_obj.image.name.split('/')[-1], my_model_obj.image.file)} form = myform(file_data) 

https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -