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)
<div id='result'>
Your text will appear here
</div>
<br>
<div id= 'record'>
<button onclick="startConverting()" class='btn btn-info btn-sm' id="re">record</button>
<button class='btn btn-info btn-sm ml-3' value="Send" id="send">Send</button>
</div>
<script>
function startConverting()
{
document.getElementById("re").style.visibility = "hidden";
var r=document.getElementById('result');
var spr=new webkitSpeechRecognition(); //Initialisation of web Kit
spr.continuous=false; //True if continous conversion is needed, false to stop transalation when paused
spr.interimResults=true;
spr.lang='en-IN'; // Set Input language
spr.start(); //Start Recording the voice
var ftr='';
spr.onresult=function(event){
var interimTranscripts='';
for(var i=event.resultIndex;i<event.results.length;i++)
{
var transcript=event.results[i][0].transcript;
transcript.replace("\n","<br>")
if(event.results[i].isFinal){
ftr+=transcript;
}
else
interimTranscripts+=transcript;
}
r.innerHTML=ftr +interimTranscripts ;
};
spr.onerror=function(event){};
}
$(document).ready(function() {
$("#send").click(function(event){
$.ajax({
type:"POST",
url:"/audio_data",
data: {
send : $('#result').html()
},
});
});
});
</script>
</div>
But changed the function in views.py a bit:
@csrf_exempt
def audio_data(request):
if request.method == 'POST':
result = request.POST['send']
return render(request, 'success.html')
elif request.method == 'GET':
return render(request, 'afterContinue.html')
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:
urlpatterns = [
path("", views.index, name="index"),
path('admin/', admin.site.urls),
path('audio_data', views.audio_data)
]
print(request.POST)output will be seen on terminal where you are running your django development server