So I have this form (some elements removed for clarity)
<form id="{{ section }}-submission-form{{ cell_id }}">
<input type=hidden name="section" value="{{ section }}" />
<input type="hidden" name="school" id="{{ section }}-submit-school{{ cell_id }}" />
{% if section == "posts" %}
<input type=hidden name="url" />
<textarea class="{{ section }}txtinput{{ cell_id }}" name="text-submission"
default="{% if is_advice %}What's your question?{% else %}What's on your mind?{% endif %}"
id="{{ section }}-suggestion-box{{ cell_id }}"
style="margin: 0 0 .5em 0;font-family: Arial, sans-serif;font-size: 14px; width: 410px;"
rows='8'></textarea>
<br />
{% endif %}
{% if section == "photos" %}
<span style='line-height: 40px;'>
<label class="photouploadlabel">URL</label><input type="text" name="image-url" style="width: 335px" /><br>
<label class="photouploadlabel">File</label><input type="file" name="image-file" style="width: 335px"/><br>
<label class="photouploadlabel">Caption</label><input type="text" id="image-caption{{ cell_id }}"
name="image-caption" style="width: 335px" default="optional"/>
</span>
{% endif %}
<div id="{{ section }}-bottomdiv{{ cell_id }}" style="height: 45px; margin-top: .5em; width: 413px;">
<div style="height: 45px">
<label id="{{ section }}-tagsbutton{{ cell_id }}"
style="margin-right: .5em; cursor: pointer; vertical-align: bottom; float:left; line-height: 1.8em;">Tags</label>
<input id="{{ section }}-tagsinput{{ cell_id }}" type="text" name="tags-list" style="position: relative"/>
<button id="send-{{ section }}-suggestion{{ cell_id }}" disabled="disabled"
style="float:right; position: relative; bottom: 7px; right: -4px;">Post</button>
</div>
The tags-list input is turned into an autocomplete and users select tags, which are then added to a global js variable “selected tags”. When the user presses “Post”, I have this code:
alert(selectedtags);
$("#"+section+"-submission-form"+cellid).ajaxSubmit({
url: '/save-suggestion/',
type: 'post',
data: {'tags': selectedtags },
dataType: 'json',
success: function(response){
clear_text(section, cellid);
location.reload();
},
Here’s the weird bit: whichever section I’m in, the alert works. However, if I print request.REQUEST on the server side, if the section is photos I get
{u'image-url': u'http://i.imgur.com/vUxla.jpg', u'tags-list': u'', u'tags': u'wtf,crazy,pics', u'section': u'photos', u'school': u'1997', u'anonymity-level': u'schoolandmajor', u'image-file': u'', u'image-caption': u''}
i.e. what I want. But if the section is posts, I get
{u'text-submission': u'wtf', u'school': u'1997', u'tags-list': u'', u'url': u'', u'section': u'posts', u'tags[]': u'crazy', u'anonymity-level': u'schoolandmajor'}
So not only is it renamed to tags[] (which I’ve seen before and am not too concerned about, but…), it also truncates my tag list to just the last entry.
Does anyone have any idea what I could be doing wrong?
EDIT: upon further inspection, this is something that using request.REQUEST is doing, as opposed to request.POST.
So, still, what is going on?
request.REQUESTis aMergedDictwhich contains aQueryDictfor POST and one for GET. When iterating the MergedDict, you will get single values for each key, which is what the__str__method does for that class. You can get around this by usingQueryDict.getlistlike so:Which in my test, yields:
If I just
print request.REQUEST['tags[]'], I get just 123.