R
R
recursy2022-04-13 10:08:26
AJAX
recursy, 2022-04-13 10:08:26

How does AJAX work with django?

Good afternoon, I want to describe in detail how I see the implementation of my project and I want to hear the opinion of programmers who understand this.

I have my own project on Django
625675e7182bc866218940.jpeg
. At first I worked according to the old methods, that when a button is pressed, a redirect occurs to the same page,
now I learned a little to work with AJAX, so that when a button is pressed, there is no redirect and the button changes.

My question is, would such a layout be a crutch code, where on the server side, there will be information that the movie is in favorites, and AJAX itself will send only flags that tell the server to add / remove the movie from favorites.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Nesterov, 2022-04-13
@recursy

where on the server side, there will be information about whether the movie is in favorites,

it will be on the server side anyway. The question is how to get it.
When the page loads, I would get all the necessary data through the context of the view that displays the page and display the appropriate buttons using templates.
And I would use js requests only for updating states (by sending requests to the necessary urls).
Another thing is that ajax, like jquery in general, is of little relevance. At a minimum, requests can use xmlhttprequest or fetch .
In general, as an alternative, you can look at frameworks like react/angular/Vue.js

J
Jack444, 2022-04-13
@Jack444

This is not a crutch, but rather a common solution. Create a new model Favorites, there are two links 1k, add a user and a movie to many, for anonymous users you can also connect the session model, but it’s better to stuff sessions into memcash. Make an additional view that takes an add and del argument and, based on the request, delete or create an entry in the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question