[Answered ]-Django: Modify database object on click using a ListView

1👍

I think it would be better to have a new endpoint
accounts/products/archive/{productPk} {productPk} being the PK of the product to archive
This has 2 benefits vs the form approach
1.That endpoint is reusable across your views with simpler calls and not using multiple forms
2.If you decide to have an API alongside your FE, you already have the archive action done, with your approach you would have to create multiple ways to archive

Coding the POST request itself is pretty straightforward based on the code you already have, just make sure all proper validations exist on the new endpoint

EDIT – more information
Create a new view that receives the PK as an argument

def archive_product(request, archive_pk):
    # Rest of the method

Add a new url path with a path parameter (django uses regex for that)

path(r'^accounts/products/archive/(?P<product_pk>\d{0,50})/$', views.archive_product)

Change your JS code to call this new URL instead of submitting a form, here you will have to code this on your own since my JS knowledge is a bit more limited to give at least almost working code

The django code is based on the docs since i didn’t want to create a new project for this. The code should already exist in your current view or is pretty simple to write

Leave a comment