0

I get the following error raise MultiValueDictKeyError(key) : django.utils.datastructures.MultiValueDictKeyError: 'redirect' when I try to save the value the user types in.

At first, I thought it had something to do with the input value, but since the error explicitly gives 'redirect' back I assume it has something to do with the redirect to the next part of the application. I have tried two different versions of return redirect, but it is very confusing what now exactly the right thing is.

At the same time, I get raise NoReverseMatch(msg) when I try to submit the input value to the database.

view

def InputData(request, element_id, session_id):
    input_element = get_object_or_404(InputData_model, pk=element_id)
    voice_service = input_element.service
    session = lookup_or_create_session(voice_service, session_id)

    if request.method == "POST":
        session = get_object_or_404(CallSession, pk=session_id)
        value = 'DTMF_input'
        result = UserInput()
        result.input_value = request.POST.get('input_value')
        result.session = session
        result.category = input_element.input_category 
        result.save()

        return redirect(request.POST['redirect'])

template

<form id="input_form">
  <property name="interdigittimeout" value="2s"/>
 <property name="timeout" value="4s"/>
 <property name="termchar" value="#" />
  <field name="input_value" type="digits?minlength=1;maxlength=5">
    <prompt>
     <audio src="{{ ask_input_label }}"/>
    </prompt>

    <filled>

     <assign name="redirect" expr="'{{ redirect_url }}'"/>
     <submit next="{{ url }}" enctype="multipart/form-data" namelist="input_value" method="post"/>
     <goto next="{{ redirect_url }}" />
    </filled>
  </field>
</form>

traceback

Internal Server Error: /vxml/InputData/33/57
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'redirect'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/vsdk/service_development/views/vse_input.py", line 55, in InputData
return redirect(request.POST['redirect'])
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'redirect'
Internal Server Error: /vxml/InputData/33/57
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'redirect'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/vsdk/service_development/views/vse_input.py", line 55, in InputData
return redirect(request.POST['redirect'])
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'redirect'
10.33.126.112 - - [29/Apr/2019:19:47:02 +0200] "POST /vxml/InputData/33/57 HTTP/1.1" 500 67237 "http://petrichor-rain-system.herokuapp.com/vxml/InputData/33/57" "Voxeo-VXML/16.0.4.5.89134"

Conclusion, I am completely confused with all the errors I get on this code and I don't know what to do anymore. Would someone want to help me make this work?

Natasja
  • 97
  • 1
  • 2
  • 9
  • 2
    Django Raises django.utils.datastructures.MultiValueDictKeyError if the key does not exist in request. what is the output of `print(request.POST)` ? – Hari May 03 '19 at 13:45
  • this just means your POST data doesn't have a "redirect" key/value pair. I don't really understand VXML so I don't really know what client is interpreting the VXML and what is supposed to be posted in a form, but for sure your form isn't submitting the "redirect" value you put in the tag. i.e. the problem is with your template or the client using it to submit data. – dirkgroten May 03 '19 at 13:58

1 Answers1

0

To help you understand these error traces, the usual way to look at them is this:

  1. Search for the line(s) that refer to files that are part of your own code:

    • So File "/app/vsdk/service_development/views/vse_input.py"
    • Not File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" or any line within the python3.6/site-packages folders

    In this case there's only one line of your code, so that's easy. If there are multiple lines, the one most at the bottom is where to start.

  2. Look at the location of the error:

    • line 55, in InputData
    • return redirect(request.POST['redirect'])

    That tells you exactly where the exception is raised

  3. Look at the exception: django.utils.datastructures.MultiValueDictKeyError: 'redirect'. This tells you more about what's wrong. KeyError means the key doesn't exist in the dictionary. Here we have a special type of dictionary (keys can have multiple values). But basically the meaning is: this key ("redirect") doesn't exist, so I cannot access it.

If you print(request.POST.get('redirect')) you'll see it's None.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • Thank you for your extensive explanation, but my next question would be how to solve it? – Natasja May 03 '19 at 16:58
  • I think just assigns a variable, it doesn’t add to the fields submitted by the form. You should look up how to submit the equivalent of hidden input fields in vxml. The problem is your vxml and I’m not familiar with vxml. – dirkgroten May 03 '19 at 18:00