4

I'm using a Django form to load an image and I'm using Bootstap-fileinput.

When the form has already been initialised with an image I want the image displayed immediately but it simple shows no image currently. How do I get the template to automatically show the file-input-exists version of the Bootstap HTML rather than the file-input-new version?

Forms.py :

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['image',]
        widget = {
            'image' : forms.FileInput(attrs={'class': 'form-control',
                                             'id' : 'input-file'}),
            }

My template has:

{{form.image}}
{{form.image.errors}}

This generates the following HTML whether the form contains an image or not

<div class="file-input file-input-new"><div class="file-preview ">
    <div class="close fileinput-remove">×</div>
    <div class="">
        <div class="file-preview-thumbnails"></div>
        <div class="clearfix"></div>    
        <div class="file-preview-status text-center text-success"></div>
        <div class="kv-fileinput-error file-error-message" style="display: none;"></div>
    </div>
</div>
<div class="kv-upload-progress hide"></div>
<div class="input-group ">
   <div tabindex="500" class="form-control file-caption  kv-fileinput-caption">
       <div class="file-caption-name"></div>
   </div>

   <div class="input-group-btn">
       <button type="button" tabindex="500" title="Clear selected files" class="btn btn-default fileinput-remove fileinput-remove-button"><i class="glyphicon glyphicon-trash"></i> Remove</button>
       <button type="button" tabindex="500" title="Abort ongoing upload" class="btn btn-default hide fileinput-cancel fileinput-cancel-button"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>

       <div tabindex="500" class="btn btn-primary btn-file"><i class="glyphicon glyphicon-folder-open"></i> &nbsp;Browse …<input class="form-control" id="input-file" name="image" type="file" required=""></div>
   </div>
</div>

** EDIT ** Thanks to help so far I now have the images showing, but if I try and resubmit the form I get a message that no file has been chosen. I need to pick this up as well.

I've added this JavaScript in order to display the image on loading.

    <script>

    $("#input-file").fileinput({
        showUpload: false,
        initialPreviewShowDelete: false,

        {% if form.instance.image %}
        initialPreview: [
            "<img src='{{ form.instance.image.url }}' class='file-preview-frame'>",
        ],
        initialPreviewConfig: [
            {caption: "{{ form.instance.image.name}}",},
        ],
        {% endif %}
    });

    </script>
HenryM
  • 5,557
  • 7
  • 49
  • 105
  • cannot understand you problem .you want to show image when you select a image in browse or initialised image is not showing? – Rohit Chopra Mar 22 '17 at 21:10
  • @Rohit Chopra - It shows the image when I select it with browse but if I want to edit the form and so I initialise it with a record it does not show the preloaded image (whereas all the other data is shown). I want it to show the image when it has been initialised from the form – HenryM Mar 23 '17 at 06:51
  • check this question it is same as yours. http://stackoverflow.com/questions/9830183/django-how-can-i-display-a-photo-saved-in-imagefield – Rohit Chopra Mar 24 '17 at 07:22
  • @Rohit Chopra - it is not the same issue. Bootstrap 'builds' the file-input tags around my form field so it displays nicely – HenryM Mar 24 '17 at 10:01
  • Alright then give some time I will upload answer tonight. – Rohit Chopra Mar 25 '17 at 12:14
  • I changed my mind, you're original question just had more scope than I first understood it to have. You're justification does make sense. If you edit your question in some way then I will be able to remove my downvote. I'm cleaning my comments up. – Greg Schmit Mar 31 '17 at 15:03

1 Answers1

1

I am getting the preview of saved database file by using this script

<script>
$(document).ready(function(){
    $("#id_album_logo").fileinput({
    showUpload: false,
    showCaption: false,
    browseClass: "btn btn-primary btn-lg",
    fileType: "any",
    previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
    overwriteInitial: false,
    initialPreviewAsData: true,
    {% if form.instance.album_logo %}
    initialPreview: [
        "{{ form.instance.album_logo.url }}",
    ],
    initialPreviewConfig: [
        {caption: "{{ form.instance.album_logo.name}}",  width: "120px", url: "{$url}", key: 1},
    ],
    {% endif %}
});
})

where id_album_logo is the id assigned to form input ,

initialPreview: [ "{{ form.instance.album_logo.url }}", ],

is the url of the image already saved in database

my models.py

class Album(models.Model):
user = models.ForeignKey(User)
album_logo=models.FileField(upload_to=upload_location)

my forms.html is

<div class="container-fluid">
<div class="row">
    <div class="col-sm-6 col-md-6 col-lg-6 col-sm-offset-3 col-md-offset-3 col-lg-offset-3">
            <form  action="" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {% include 'form-template.html' %}
                    <div class="form-group">
                        <br>
                        <button  type="submit" class="btn btn-success">Submit</button>
                    </div>
            </form>
    </div>
</div>

and form-template.html is

{% for field in form %}

<div class="form-group djfr">
        <label class="control-label">{{ field.label_tag }}</label>
        {{ field }}
    <span class="text-danger small">{{ field.errors }}</span>
</div>

{% endfor %}
Deep 3015
  • 9,929
  • 5
  • 30
  • 54
  • I've tried copying this into my template, replace 'album_logo' with 'image' and I'm still not getting anything loaded – HenryM Mar 27 '17 at 09:48
  • I can see the image url within the script so that is working, but I don't seem to have any id appearing from my `{{ form.image }}` call which should be the same as your `{{ field }}`. This is very nearly the solution – HenryM Mar 27 '17 at 10:00
  • so get id (form input file field) by inspect element developer tools in chrome and apply that id to script – Deep 3015 Mar 27 '17 at 10:07
  • That's what I've done. The only id I have within that area is `` so I've used `$("#input-file").fileinput({`... but it isn't loading – HenryM Mar 27 '17 at 10:32
  • A couple of images it cannot find (because they're not there). Nothing related to this – HenryM Mar 27 '17 at 10:35