1

I have a file field from which I am uploading image. Now after uploading the image whthout saving it I want to show it in the template. Like if user browse and selects the photo then that selected photo should be shown in them form.

I am doing something like :

<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{form.image.value}}
{% if form.image.value %}
<br/><img src="{{ form.image.value.url }}" width="240"></img></br>
{% endif %}
{{form.as_p}}

<input type="submit" name="submit" value="Upload">
</form>

and my model:

class Album(models.Model):
image = models.FileField(upload_to=get_upload_file_name)

How can I show the form field value after selecting it ?

Thanks in advance

varad
  • 5,407
  • 14
  • 45
  • 95
  • I think it's not clear when you say "without saving it": the upload happens only after submitting your form (through your submit input "Upload"). So, in some sense, you are saving it. What is that you are not saving? Perhaps the form of your snipped is part of a bigger form (a `ModelForm`, perhaps?) and you don't save the outer form? I suggest you to clarify your context ... on the other side, my feeling is that you are going to require some js/ajax in order to upload your image "immediately", without your user POSTing the other fields – FSp Oct 27 '15 at 11:09
  • If you're wanting to preview the image before persisting the model, have a look at: http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – Brandon Oct 27 '15 at 13:15

0 Answers0