So I am converting a site from PHP(Codeigniter) to Django, figured it would be a good way to learn. I can figure most issues out however I am stuck on this one.
In PHP i can $e->estimate = json_encode($_POST['estimate']);
to store an HTML input array as JSON encoded strings in the DB, does python/django offer any similar functionality.
I am looking at simplejson.dumps(request.POST['estimate']) but that throws a MultiValueDictKeyError
The POST contains items like estimate[discount]
estimate[tax]
estimate[shipping]
So can Django/Python do the above, take an HTML input array, JSON econde it and store it in the DB
After having a look at the documentation in django I found that request.POST is an QueryDict object.
at https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict .
As a dictionary (JSON object or Python dict) cannot have more than one key the JSON serialiser is throwing the MultiValueDictKeyError saying that you have multiple keys that are the same.
Not sure how php was doing it, but a fix would be either of the following
Copy the POST QueryDict and modify the contents so there isn’t any duplicate keys.
Change the form data that is being submitted.
The reason for the QUeryDict is that for forms being submitted you might validly get multiple keys from the form, for multi-select fields etc.
hope that helps.
Mark