将图像 URL 放到 Crud 的更新页面上

问题描述 投票:0回答:1

这是一个CRUD编辑页面的模板页面,当我们渲染这个编辑页面时,除了图像之外,所有已经上传的字段都会显示出来,我们如何在图像字段中看到已经上传的图像,希望你们明白了。

<body>
    <div class="row">

        <div class="col-sm-4">
            <h1 class="text-center alert alert-info">Modify Order Details</h1>
            <form action="" method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                <label for="category">Select a Category :- </label>
                    <select name="category" id="category">
                    <option value="None"selected >None</option>  
                    <option value="Tshirt" {% if products.category == 'Tshirt' %} selected {% endif %}>Tshirt</option>
                    <option value="Jeans" {% if products.category == 'Jeans' %} selected {% endif %}>Jeans</option>
                    <option value="Jacket" {% if products.category == 'Jacket' %} selected {% endif %}>Jacket</option>
                    <option value="Shoes" {% if products.category == 'Shoes' %} selected {% endif %} >Shoes</option>
                    <option value="Watch" {% if products.category == 'Watch' %} selected {% endif %} >Watch</option>
                    </select>   <br><br>
                    
                    <label for="product_name">Select a Product: </label>
                    <select name="product_name" id="product_name" value="{{products.product_name}}" >
                    <option value="{{products.product_name}}" selected readonly >{{products.product_name}}</option> 
                    
                    </select><br><br><br>


                <label for="product_price">Product Price:</label>
                <input type="text" id="product_price" class="form-control" name="product_price" value="{{products.product_price}}" pattern="^[^ ].+[^ ]$" title="Please Remove Extra Spaces" required><br>

                <label for="supplier_images">Image:</label>
                <input type="file" name="image"></a> <br><br>
                <!-- <span>Current image:</span> <img src="{{products.image.url}}" alt="" width="40"> -->
                <br><br>

                <input type="submit" value="Update" class="btn btn-success">
            </form>
        </div>

    </div><br><br>
    <h3><a href="{% url 'supplier_dashboard' %}">Back</a></h3>
javascript python html django ajax
1个回答
0
投票

假设您使用 django 模板来显示上传的图像,您可以这样做:

{% if products.image.url %}
   <span>Current image:</span> <img src="{{products.image.url}}" alt="Current Image" width="100">
{% endif %}
© www.soinside.com 2019 - 2024. All rights reserved.