[Answer]-Why replaced div content gone after refresh?

1👍

Any markup that you add via JavaScript will only be present for one request cycle. If you perform another GET request, even if to the same url, the dynamically added markup will not be present in the DOM.

To do that, you have a couple of options:

  1. Persist the data from the ajax post in the database, session, etc.
  2. Persist the data using localStorage on the client-side.

If you persist it server-side, you can render it out using Django template tags. If you persist it in localStorage, you would need to render it via JavaScript. Which one you choose completely depends on the needs of your application.

0👍

i would do this way:

<span class="logo">
  <div id="pre-user">
      {% if user.is_authenticated %}
          Welcome <strong>{{ user.email }}</strong>
      {% else %}
          Welcome <strong>Guest</strong>
      {% endif %}  
  </div>            
</span>

this way, you can keep your js which just changes the #pre-user‘s html content for now, if user reloads the page, the changed content will stay there since it is now backend-connected.

Leave a comment