I have a Django CreateView that works as expected. I allow the user to attach a photo and then after they click Submit, the view captures the image as expected. However, I am trying to figure out if there is a way, using CreateView, to show the image that has been captured. As of right now, it shows the file name, but I am trying to get it to show the image instead. I tried a variation of this SO question, but it is not for CreateView and I can't figure out how to translate it. Render image without saving
Here is my simple example, that works as expected.
My Model...
class NewAuthor(models.Model):
image = models.ImageField(upload_to='images/',default='images/pic.jpg')
My view....
class CreateNewAuthorView(CreateView,NeverCacheMixin):
model = NewAuthor
form_class = CreateNewAuthorForm
template_name = 'create_new_author.html'
My form...
class CreateNewAuthorForm(forms.ModelForm):
class Meta:
model = NewAuthor
fields = ['image',]
My HTML...
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="picture">
{{ form.image }}
</div>
How can I incorporate code that will allow the image that was uploaded to actually be displayed before the user hits submit?
Thanks in advance for any suggestions.