Opened 5 years ago

Closed 5 years ago

#17191 closed Bug (invalid)

QueryDict appears incorrect on POST.

Reported by: otto@… Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords: QueryDict, POST, MultiValueDictKeyError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The following is test code stripped to bare essentials. When run with
">python runserver".
The values entered in the form at the T1 and T2 fields are "t1-value"
and "t2-value" respectively.

The following error is produced:

MultiValueDictKeyError at /test_view/
"Key 't1' not found in <QueryDict: {u'Bname': [u'Bval\\r\\nt1=t1-value\\r\\nt2=t2-value\\r\\n']}>"

Code that produces this is:

##### BEGIN CODE #####
from django.http import HttpResponse
from django import template     
from django.views.decorators.csrf import csrf_exempt

data = \
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "">
    <form enctype="text/plain" method="POST" action="/test_view/">
        <button value="Bval" name="Bname">TEST</button><br><br>
        T1 &nbsp;<input name="t1" value=""/><br>
        T2 &nbsp;<input name="t2" value=""/><br><br>

def test(request):    
    return HttpResponse(data)

def test_view(request):
    t1 = request.POST['t1']
    t2 = request.POST['t2']
    return HttpResponse("Returns %s and %s OK with %s " % (t1,t2,"POST"))
##### END CODE #####

Moving the button and input fields around shows that the
"QueryDict" key is always the
first field name in the form "Bname" above. Moving the button to follow the entry fields gets following error:

MultiValueDictKeyError at /test_view/
"Key 't2' not found in <QueryDict: {u't1': [u't1-value\\r\\nt2=t2-value\\r\\nBname=Bval\\r\\n']}>"

Changing POST to GET throughout makes form work.

Change History (1)

comment:1 Changed 5 years ago by Aymeric Augustin

Resolution: invalid
Status: newclosed

The text/plain encoding isn't supported by Django.

Based on the HTML spec, I don't think it's valid. Use application/x-www-form-urlencoded (the default) instead.

Note: See TracTickets for help on using tickets.
Back to Top