python - 错误 : didn't return an HttpResponse object. 它返回 None

标签 python django button

我想按 html 模板中的按钮

              <form action="{% url 'speech2text' %}" class="card-text" method="POST">
                  {% csrf_token %}
                  <button class="btn btn-primary btn-sm" type="submit">Start</button>
              </form>

              <p> Sie haben das folgende gesagt: </p>
              <p> {{ speech_text }} </p>

按下按钮,将执行以下 View 中的代码并将结果发送回模板:

def speech2textView(request):
    if request.method == "POST":
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            text = r.recognize_google(audio, language="de-DE")
            args = {'speech_text': text}
            return render(request, 'speech2text.html', args)

这里出了什么问题?非常感谢您的帮助。

最佳答案

我再次编辑了代码,现在它可以工作了。我想分享这个。非常感谢这个社区。

def speech2textView(request):
    args = {}
    if request.method == "POST":
        print(request.method)
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            text = r.recognize_google(audio, language="de-DE")
            args = {'speech_text': text}
    return render(request, 'speech2text.html', args)

关于python - 错误 : didn't return an HttpResponse object. 它返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549947/

相关文章:

java - 如何在Onitemclicklistener函数接口(interface)上实现多个按钮?

java - 音乐无法按一下按钮播放

python - 在 PyQt5 中停止工作线程中的无限循环最简单的方法

python - 是否有一个相当于 PyXll `@xl_func(macro=True)` 和 xlwings 的装饰器?

Django 渲染单选按钮选项以及来自模型的额外信息

python - 通过 Helicon Zoo 在 IIS 上部署 Django 项目

css - 浏览器正在下载页面上已加载的背景图片?

python - 使用 Python 的 elementtree 为带有文本的节点添加书尾

python - 从 SQLalchemy 中的自引用表创建树

javascript - Django - 没有 JQuery 库的 Ajax