M
M
mefisto6612019-12-13 09:41:08
Django
mefisto661, 2019-12-13 09:41:08

AJAX with DJANGO, what is possible and what is not?

Just starting to understand ajax + django. In the template, I have the following logic: if the user is not authorized (if user.is_anonymous), a registration form is displayed, (if user. is_authenticated) a welcome message and an exit button are displayed. Accordingly, I want to make authentication without reloading the page. The next question is, as far as I understand, in order to check the conditions, you need to reload the template? Therefore, the only way is to return data to the ajax request, for example, the user's login in case of successful authorization, and then in js - to replace the login form with a greeting. Or you can still achieve a "rebuild" of the entire page, without reloading the page). All that I found on the Internet and documentation - return to ajax httpresponse or jsonresponse

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2019-12-13
@mefisto661

Therefore, the only way is to return data to the ajax request, for example, the user's login in case of successful authorization, and then in js - to replace the login form with a greeting.

This is the correct approach.

F
FeNUMe, 2019-12-13
@FeNUMe

How do you imagine rebuilding the already loaded html from the server side? Here, either an ajax request and editing the block using js, or reloading the page after receiving a response to the request.
You do not need to return the data, the "success/error" status is enough, because you already have the user's login, you send it for verification.

A
antonksa, 2019-12-16
@antonksa

As Sergey said - you are on the right track. Just start using JS frameworks like Vue right away, this will save you a couple of pounds of nerves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question