'Django: set dynamic array in urlpatterns from urls.py
I have this function that I want to use in order to delete some records from a database. It takes as input an array of ints.
function deleteScript(idList){
$.ajax({
url: '/delete',
type: 'get',
data: {
ids: idList
},
success: function(response) {
alert('success')
}
})
console.log('ajax sent')
}
How can I set the django dynamic url so that no matter the list, the request would always call the same method (e.g. http://localhost:8000/delete/?ids%5B%5D=1&ids%5B%5D=2 http://localhost:8000/delete/?ids%5B%5D=5 would go to delete/)?
urls.py
urlpatterns = [
path('', views.get_data),
path('delete/<list:ids>', views.delete)
]
Solution 1:[1]
You can pass the id list as comma separated values:
?id=1,4,5
And then parse it in Python to a list.
id_param = request.GET['id']
try:
ids = [int(a) for a in id_param.split(',')]
except ValueError:
# handle improper request
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | maciek.glowka |
