1

I'm beginning to learn web development with Django and I've run into a problem, I have a form :

from django import forms
from .models import film

class searchFilm(forms.Form):
    FilmName = forms.Field()

and in my views file I try to print the search term:

print request.POST["FilmName"]

but I get the error:

raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'FilmName'"

if I just print the whole thing I get the output:

<QueryDict: {u'csrfmiddlewaretoken': [u'************'], u'FilmName': [u'hellowe']}>

And I'm not sure why, how do I just print the search term?

user2320239
  • 1,021
  • 2
  • 18
  • 43
  • 1
    Take a look here http://stackoverflow.com/questions/11336548/django-taking-values-from-post-request – trinchet Apr 20 '16 at 14:17
  • I've looked through those comments and I'm still stuck, if I loop through the POST dictionary to print all keys, I get 'csrfmiddlewaretoken' and 'FilmName', so surely doing print request.POST["FilmName"] should print the text entered into the field? – user2320239 Apr 20 '16 at 14:38
  • 1
    Django raises `MultiValueDictKeyError` if you try to access values which are not in the POST dictionary. You should use `print request.POST.get('FilmName', None)` It will print `None` if `'FilmName'` is not in POST dict. – xyres Apr 20 '16 at 14:52
  • Can you show the complete view? – Alasdair Apr 20 '16 at 14:57
  • If you're using a form, you should be accessing `form.cleaned_data` rather than the raw POST, anyway. – Daniel Roseman Apr 20 '16 at 15:07

0 Answers0