How to pass array to django views

Hello everyone,

I created a web app to make crud operations. I’m selecting a column and I want to update all values in that column. I made a button for this and I keep the ids of the rows in the selected column in an array(jquery). But I don’t know how to pass this array to django views. If I can pass it, I want to update the values corresponding to this column and row in the database. I did something like below but having 404 not found error. Any ideas?

Thanks in advance.

base.html

$('#extension').click(function () {
            // console.log(table.rows('.selected').data()[0].id);
            var nplainArray = []
            var plainArray = table.rows().data().toArray();
            for (var i = 0; i < plainArray.length; i++) {
                nplainArray.push(plainArray[i].id)
            }
            $.ajax({
              type: 'POST',
              url: '/edit_column/',
              data: {'nplainArray[]': nplainArray},
            });
        });

views.py

def edit_column(request):
    if request.method == "POST":
        nplainArray = request.POST.getlist('nplainArray[]')

    messages.success(request, "Column data are updated successfully!")

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('edit_row', views.edit_row, name='edit_row'),
    path('edit_column', views.edit_column, name='edit_column'),
    path('delete_row/<str:row_id>', views.delete_row, name='delete_row'),
]

Why do you need jquery for this? I think a better approach is to use a Django form set, with a checkbox in each in order to indicate the elements you want to select.

I think your 404 is caused by the trailing slash on /edit_column/ in base.html. Here:

I think changing it to url: '/edit_column', will get it working.

1 Like

Thank you very much! I was a silly mistake.