Pre-populate edit form with existing product description in Django

I'm building a Django inventory app and have a Product model with various fields. I've implemented an edit button for each product row that allows users to update information. However, when clicking the edit button, the form appears empty for all fields.

I want the edit form to automatically display the existing product information for each field, so users only need to modify specific details. This eliminates the need to retype everything.

I've searched the Django documentation on forms, but I'm unsure how to achieve this pre-population for the edit functionality.

How can I pre-populate the edit form with the existing product information in my Django inventory app?

def edit_product(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'POST':
        form = ProductForm(request.POST, instance=product)
        if form.is_valid():
            form.save()
            return redirect('product_list')
    else:
        form = ProductForm(instance=product)
    return render(request, 'inventory/edit_product.html', {'form': form}) 

enter image description here enter image description here

Back to Top