views:

54

answers:

1

Hi,

can I access an image via a form-field in the template?

<p>
{% if field.errors %}
<span class="error">{{ field.errors }}</span>
{% endif %}

{{ field.label_tag }}:<br />

<img src="{{ field.value.url }}" /> {# <- This is what I want to do, I know it doesn't work like this #}

{{ field }}<br />
<span >{{ field.help_text }}</span>
</p>

thanks for your time.

A: 

If you wish to display a preview, when user chooses an url in a form, then you must use some javascript. Django forms are static html elements, you must hook some javascript function to the field, which will change image src, when user chooses new image.

gruszczy
sorry for being unclear. i just want to display an uploaded image close to the form field.
Till Backhaus
What do you exactly mean? You have a form for uploading image. Do you want to display it before user hits submit button or afterwards? Where is image or image url stored?
gruszczy
I want to display an image that was uploaded to server already and is stored there. The path to the image is stored in `model.field`, the field is `modelform.field` for this example.
Till Backhaus