Skip to content Skip to sidebar Skip to footer

After The Added Product Is Updated And Edited, Another New Detail Page Will Be Generated, But The Original Modified Page Was Not Updated

Python 3.8.3 Django 2.2 asgiref 3.3.1 djangorestframework 3.11.1 Pillow 7.2.0 pip 19.2.3 psycopg2 2.8.

Solution 1:

The problem is that you are creating a new object instead of updating the existing one: Hope this will solve your problem.

defproductUpdate(request, id=None):
    # basic use permissions 基本使用權限ifnot request.user.is_staff ornot request.user.is_superuser:
        raise Http404

    context = {}
    if request.POST:
        form = ProductForms(request.POST)
        if form.is_valid():
            instance = get_object_or_404(Product, id=id)
            form = ProductForms(data=request.POST, instance=instance)
            form.save()
            # updating our context
            context.update({'instance': instance})
            # message success
            messages.success(request, "<a href='#'>Item</a> Saved",
                             extra_tags='html_safe')
            return HttpResponseRedirect(instance.get_absolute_url())

    else:
        form = ProductForms()  # if request isn't POST we initialize an empty form

    data = cartData(request)
    cartItems = data['cartItems']

    context.update({
        'form': form,
        'cartItems': cartItems,
    })
    return render(request, 'store/form.html', context)

Visit django's modelform docs for more detail.

Post a Comment for "After The Added Product Is Updated And Edited, Another New Detail Page Will Be Generated, But The Original Modified Page Was Not Updated"