0

I know its basic but I got stuckl because of it I internet did not help me.

This is snippet of my django class based view (django 1.7)

  def post(self, request,*args, **kwargs):
        context =  RequestContext(request, {})
        print request.POST
        print request.POST['search_text']

In html

  <form method="post" action="{% url 'storage_place' %}">{% csrf_token %}
    <div class="container">
      <div class="row">

        <div class="col-md-4">
          <div class="form-group">
            <input type="text" class="form-control" id="search_text" name="search_text " placeholder="Key words...">
          </div>
        </div>
        <div class="col-md-3">
          <div class="s_btngroup">
            <div class="col-md-6 col-sm-4 col-xs-5">
              <input type="submit" class="btn ft_default-btn-red ft_primary-btn-mini" value="Search" /> 
            </div>

          </div>
        </div>
      </div>
    </div>
    </form>

When I do

print request.POST

It is printing

QueryDict: {u'csrfmiddlewaretoken': [u'GInHZCd4UK8oWjs2txgppCNEof3VC8zy'], u'search_text ': [u'defrghj']}

But in very next line when I do

print request.POST['search_text']

I am getting multivalue dict error.

Please tell me what would be the reason for this.

Django Man
  • 87
  • 1
  • 7
  • Possible duplicate of [django MultiValueDictKeyError error, how do i deal with it](https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – Thierry Lathuille Jul 03 '17 at 18:07

1 Answers1

1

There's a trailing space after 'search_text ' in the <input.../>'s name attribute in your template:

<input type="..." class="..." id="..." name="search_text "
                                                        ^

And you'll notice it appears in the QueryDict with the trailing space. You should remove the space.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139