0

Is there a way in django to update the same page with the response without totally rendering it. I am trying to create a code editor to test. But when I am returning the results my contents are removed. I understand it is because I am rendering the page . I need the contents to retain. How can I do it using redirect? I am including the render statement I used and a screenshot of how it looks here:

Steps:

  1. Handle post request
  2. Program execution code
  3. Save the result in a variable called "message". Then I used

return render(request, 'editor.html', {'message': message})

I want to redirect the message to the same page without rendering a new page.

[Before submission][1] [After submission][2] [1]: https://i.stack.imgur.com/BxoLU.png [2]: https://i.stack.imgur.com/uiEOU.png

Any help will be appreciated. Thank you.

RKrishna
  • 3
  • 3
  • look into ajax. there are plenty of tutorials and questions out there https://stackoverflow.com/questions/21133135/django-update-part-of-the-page – hansTheFranz Jan 19 '21 at 15:39

2 Answers2

0

For that, you have to switch to a different web software paradigm called "single page application", which implies that both, backend and frontend, are functional software components on their own, instead of having a "dumb" HTML frontend that only displays what the backend renders.

In a regular web application, the front end is served from a backend with all the information that is going to display. In a Single Page Application, the front end is served by a server independent of the backend server, and the frontend and backend interact through an API served by the backend.

With this architecture, the frontend component is responsible for requesting and providing data from and to the backend, as well as for displaying the data and getting user's interaction, and the mean for interchanging data with the backend is called an ajax, that is an asynchronous request.

The only language accepted by web browsers is javascript, but there are many frameworks and second level languages that can render a javascript application, like React, Angular, Vue, and many others.

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
0

it is possible. using ajax in front-end and render or render_to_string in back-end(Django). using ajax you're able to call a url (with/without data you want to send), then in the views.py you can do anything needed and return render(request,'template.html, context). then in ajax you have success: function (res) { $('#id_of_element').append(res)}. this function in ajax will append the recived response which is a rendered HTML template to the target element.

mh-firouzjah
  • 834
  • 1
  • 6
  • 15