Skip to content
Advertisement

Django/Js: how to post a form without reloading whole page

My application currently flows through 3 pages:

  1. User selects question in index page
  2. User submits answer in answer page
  3. User is presented with result in results page.

I want to compress that down to a single page where the user submits an answer to the question and result is shown on the same page.

The following django-template code separates questions with Bootstrap accordion. How do I post the form without refreshing the whole page? I want to be able to display the result on the page, update CSS styling with Javascript etc.

JavaScript

Here is the view:

JavaScript

Advertisement

Answer

You can try this:

Add the below script in your template:

JavaScript

write a script and a function inside it to submit the form data.

JavaScript

Change type of the submit button and add an onclick event to call the submitData() function and pass the challenge id to it. And remove the action attr from the form. see below:

JavaScript

Return a JsonReponse to the ajax call from the views.

views.py

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement