Skip to content
Advertisement

AJAX post request with django and python

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

But changed the function in views.py a bit:

JavaScript

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

Advertisement

Answer

You can try this:

Add the jQuery CDN minified inside the <head> tag in html template:

JavaScript

Html template

  1. send a parameter save to audio_data view so that it could recognise that it has to save the audio text.
  2. send the ajax request data in json format.
  3. Check the response sent by django view was true or not.
  4. redirect the user to /audio_data?success=true if the response from view was true.
JavaScript

views.py

  1. Check if the save parameter is true or not.
  2. Check if audio text is provided or not.
  3. Return a JsonResponse with a dictionary that holds the value of ans variable that is True if the audio text is saved & is False if the text is nor provided.
  4. Render the success.html template if the success parameter is true.
  5. If both save and success parameters are False or not provided then render afterContinue.html template.
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement