68👍
✅
From here
This is a feature, not a bug. If you want a list of values for a key, use the following:
values = request.POST.getlist('key')
And this should help retrieving list items from request.POST in django/python
10👍
The function below converts a QueryDict object to a python dictionary. It’s a slight modification of Django’s QueryDict.dict() method. But unlike that method, it keeps lists that have two or more items as lists.
def querydict_to_dict(query_dict):
data = {}
for key in query_dict.keys():
v = query_dict.getlist(key)
if len(v) == 1:
v = v[0]
data[key] = v
return data
Usage:
data = querydict_to_dict(request.POST)
# Or
data = querydict_to_dict(request.GET)
- [Django]-Django: Make certain fields in a ModelForm required=False
- [Django]-What is the way to ignore/skip some issues from python bandit security issues report?
- [Django]-Django Model Field Default to Null
4👍
You can use getlist method
data = request.POST.getlist('form_4606-parentspass_id','')
- [Django]-'Request header field Authorization is not allowed' error – Tastypie
- [Django]-Django include template from another app
- [Django]-Direct assignment to the forward side of a many-to-many set is prohibited. Use emails_for_help.set() instead
1👍
This can occur when accidentally posting JSON data with the content-type multipart/form-data
, rather than application/json
.
I hit this situation when using the Django test HTTP client to create requests against a JSON API and calling:
data = {'a': [1, 2, 3]}
client.post('/api/endpoint/', data=my_data)
rather than:
data = {'a': [1, 2, 3]}
client.post('/api/endpoint/', data=my_data, content_type='application/json')
- [Django]-No module named MySQLdb
- [Django]-Docker/Kubernetes + Gunicorn/Celery – Multiple Workers vs Replicas?
- [Django]-Django: TypeError: 'tuple' object is not callable
0👍
The simplest way
{**request.GET}
{**request.POST}
or, if you use djangorestframework
{**request.query_params}
- [Django]-How to check the TEMPLATE_DEBUG flag in a django template?
- [Django]-How to use "OR" using Django's model filter system?
- [Django]-How to check if an element is present in a Django queryset?
Source:stackexchange.com