How to update new image in django

How to remove old image and update / upload new image in django.

We have inserted and fetched the image in the previous posts. Let's write the code to edit and update new image.

In the index.html template, we will give the url to edit the image as shown below:

     <a href="{% url 'edit-prod' %}" class="btn btn-success badge">EDIT</a>

Then create the path in the file as shown below :


We are passing the id as pk in the url and we will filter the record using the id. In the file, create a editProduct function and add the below code:

def editProduct(requestpk):
    prod = Item.objects.get(id=pk)

    if request.method == "POST":
        if len(request.FILES) != 0:
            if len(prod.image) > 0:
            prod.image = request.FILES['image'] = request.POST.get('name')
        prod.description = request.POST.get('description')
        prod.price = request.POST.get('price')
        messages.success(request"Product Updated Successfully")
        return redirect('/')

    context = {'prod':prod}
    return render(request'products/edit.html'context)

Open the edit.html file and paste the below code:

{% extends 'products/layouts/main.html' %}

{% block content %}

<section class="section">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h2 class="fw-bold">Edit Product</h2>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                            {% csrf_token %}
                            <div class="mb-3">
                                <label for="" class="form-label">Name</label>
                                <input type="text" Required name="name" value="{{ }}" class="form-control">
                            <div class="mb-3">
                                <label for="" class="form-label">Descritpion</label>
                                <input type="text" Required name="description" value="{{ prod.description }}" class="form-control">
                            <div class="mb-3">
                                <label for="" class="form-label">Price</label>
                                <input type="text" Required name="price" value="{{ prod.price }}" class="form-control">
                             <div class="mb-3">
                                {% if prod.image %}
                                    <img src="{{ prod.image.url }}" alt="image here" class="prod-size">
                                {% endif %}
                                <label for="" class="form-label">Image</label>
                                <input type="file" name="image" class="form-control">
                            <button type="submit" class="btn btn-warning">Update</button>
{% endblock content %}

Watch the video given above for detailed explaination and video demo. 

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT