Django loading boot file

I use the Djangoimage upload form and I use Bootstap-fileinput.

When the form has already been initialized with the image, I want the image to be displayed immediately, but it just does not display the image at present. How to get a template to automatically display the file-input-existsBootstap HTML version instead of the version file-input-new?

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 the help so far I am now showing the images, but if I try and resubmit the form, I get a message that the file is not selected. I need to do this too.

I added this javascript to display the image at boot time.

    <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>
+4
1

, 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 %}
});
})

id_album_logo - , ,

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

- URL- ,

my models.py

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

my forms.html

<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>

form-template.html

{% 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 %}
0

Source: https://habr.com/ru/post/1672960/


All Articles