Reputation: 45
I want to press a button in an html-template
<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>
by pressing the button, the code in the following view shall be executed and send back the result into the template:
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)
Whats wrong here? many thanks for your help.
Upvotes: 1
Views: 78
Reputation: 45
I edited the code again and now it is working. I would like to share this. Many thanks to this community.
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)
Upvotes: 1
Reputation: 198
try to checking the context with adding a print command and inspecting in your command window like this
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}
print(args)
return render(request, 'speech2text.html', args)
if you didn't see the data in your command window I think the problem is in your recognition step or on the request step keep tracking with printing some data to see where is the code stuck or fail.
Upvotes: 0
Reputation: 54
This error occurs because there was no return! It seems that return render(request, 'speech2text.html', args)
doesn't work. Try adding return render()
outside of the with
, and if the same error occurs try adding that outside of the request.method == "POST":
. This way u can check where the problem occurs.
Upvotes: 0