I am trying to build a web app on django where a user can record something which then gets turned into text and after that sent to the server with AJAX. I did some research and found this article: https://medium.com/@manask322/implementing-speech-to-text-feature-in-a-django-app-f0fa53bb3e03
So I copied the html code: (this is only the part I copied)
JavaScript
x
51
51
1
<div id='result'>
2
Your text will appear here
3
</div>
4
<br>
5
<div id= 'record'>
6
<button onclick="startConverting()" class='btn btn-info btn-sm' id="re">record</button>
7
<button class='btn btn-info btn-sm ml-3' value="Send" id="send">Send</button>
8
</div>
9
10
<script>
11
function startConverting()
12
{
13
document.getElementById("re").style.visibility = "hidden";
14
var r=document.getElementById('result');
15
var spr=new webkitSpeechRecognition(); //Initialisation of web Kit
16
spr.continuous=false; //True if continous conversion is needed, false to stop transalation when paused
17
spr.interimResults=true;
18
spr.lang='en-IN'; // Set Input language
19
spr.start(); //Start Recording the voice
20
var ftr='';
21
spr.onresult=function(event){
22
var interimTranscripts='';
23
for(var i=event.resultIndex;i<event.results.length;i++)
24
{
25
var transcript=event.results[i][0].transcript;
26
transcript.replace("n","<br>")
27
if(event.results[i].isFinal){
28
ftr+=transcript;
29
}
30
else
31
interimTranscripts+=transcript;
32
}
33
r.innerHTML=ftr +interimTranscripts ;
34
};
35
spr.onerror=function(event){};
36
}
37
$(document).ready(function() {
38
$("#send").click(function(event){
39
$.ajax({
40
type:"POST",
41
url:"/audio_data",
42
data: {
43
send : $('#result').html()
44
},
45
46
});
47
});
48
});
49
</script>
50
</div>
51
But changed the function in views.py a bit:
JavaScript
1
10
10
1
@csrf_exempt
2
def audio_data(request):
3
if request.method == 'POST':
4
result = request.POST['send']
5
return render(request, 'success.html')
6
7
8
elif request.method == 'GET':
9
return render(request, 'afterContinue.html')
10
The recording works and it also displays the text you say but when you click on send nothing happens. Does someone know why?
UPDATE: This is the urls.py part:
JavaScript
1
6
1
urlpatterns = [
2
path("", views.index, name="index"),
3
path('admin/', admin.site.urls),
4
path('audio_data', views.audio_data)
5
]
6
Advertisement
Answer
You can try this:
Add the jQuery CDN minified inside the <head>
tag in html template:
JavaScript
1
11
11
1
<html>
2
<head>
3
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
4
</head>
5
<body>
6
<div id="result">
7
.
8
</div>
9
</body>
10
</html>
11
Html template
- send a parameter
save
toaudio_data
view so that it could recognise that it has to save the audio text. - send the ajax request data in json format.
- Check the response sent by django view was true or not.
- redirect the user to
/audio_data?success=true
if the response from view was true.
JavaScript
1
18
18
1
$(document).ready(function() {
2
$("#send").click(function(event){
3
$.ajax({
4
url: "/audio_data",
5
data: {
6
'save': true, // Change No.1
7
'text': $('#result').html()
8
},
9
dataType: 'JSON', // Change No.2
10
success: function(response) { // Change No.3
11
if (response.ans) { // Change No.3
12
location.href = '/audio_data?success=true'; // Change No.4
13
}
14
}
15
});
16
});
17
});
18
views.py
- Check if the save parameter is true or not.
- Check if audio text is provided or not.
- Return a
JsonResponse
with a dictionary that holds the value ofans
variable that isTrue
if the audio text is saved & isFalse
if the text is nor provided. - Render the
success.html
template if thesuccess
parameter istrue
. - If both
save
andsuccess
parameters areFalse
or not provided then renderafterContinue.html
template.
JavaScript
1
23
23
1
from django.http import JsonResponse
2
3
@csrf_exempt
4
def audio_data(request):
5
data = request.GET
6
7
if data.get('save', False): # Change No.5
8
ans = True
9
10
text = data.get('text', False) # Change No.6
11
if text: // Change No.6
12
print('Text = ', text)
13
""" Here save the text the way you want. """
14
else:
15
ans = False
16
17
return JsonResponse({'ans':ans}) # Change No.7
18
19
elif data.get('success', False): # Change No.8
20
return render(request, 'success.html')
21
22
return render(request, 'afterContinue.html') # Change No.9
23