使用POST VXML引发MultiValueDictKeyError(key)

时间:2019-05-03 13:32:27

标签: python django vxml

我收到以下错误 引发MultiValueDictKeyError(key) :django.utils.datastructures.MultiValueDictKeyError:'重定向' 当我尝试保存用户键入的值时。

起初,我认为它与输入值有关,但是由于错误显式地给出了“重定向”的信息,因此我认为它与对应用程序下一部分的重定向有关。我尝试了两种不同版本的返回重定向,但是现在完全正确的事情非常令人困惑。

同时,当我尝试将输入值提交到数据库时,我得到了NoReverseMatch(msg)。

视图

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'])

模板

<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>

回溯

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"

结论,我对在此代码上遇到的所有错误完全感到困惑,而且我不知道该怎么办了。有人想要帮助我完成这项工作吗?

1 个答案:

答案 0 :(得分:0)

为帮助您了解这些错误跟踪,通常的查看方式如下:

  1. 搜索引用属于您自己代码一部分的文件的行:

    • 所以File "/app/vsdk/service_development/views/vse_input.py"
    • 不是File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py"python3.6/site-packages文件夹中的任何行

    在这种情况下,您的代码只有一行,因此很容易。如果有多行,则最底端的一行是从哪里开始。

  2. 查看错误的位置:

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

    这可以准确告诉您在哪里引发异常

  3. 查看异常:django.utils.datastructures.MultiValueDictKeyError: 'redirect'。 这可以告诉您更多有关问题的信息。 KeyError表示该键在词典中不存在。这里我们有一种特殊的字典类型(键可以有多个值)。但基本上的含义是:该密钥(“重定向”)不存在,所以我无法访问它。

如果您print(request.POST.get('redirect')),您会看到它是None