starcorn
starcorn

Reputation: 8551

Django: Does it support changes in DOM?

I wonder if Django native support changes in DOM. I don't know if it is correct name for it now so I guess I explain it instead. For example if I make an e-shop site with django. I want when I click on a product it should add to the basket, which is in html maybe looks something like this. So for each product I add a new <li></li> is added dynamically. Can I do that with django. Or do I have to use Javascript for it?

<div id="basket">
  <ul>
     <li>
     // some product
     </li>
  </ul>
</div>

Upvotes: 0

Views: 514

Answers (2)

freakish
freakish

Reputation: 56557

It depends.

1) You may want to make you app very dynamic, so another element apears in your basket without page reload. This will be done by combining ajax requests (your server needs to know what you have in basket) with DOM manipulation (purely JavaScript);

2) You can use more classical approach. Adding element to basket is just a POST request. Django handles the request (stores in session or somewhere else current basket) and generates new HTML for you.

Imho, the first approach is a way faster and it looks better for the end-user. The downside is that you may loose track of some valuable information which automatically updates when user reloads entire page (like for example the price of an item). But this should not be a problem if we're talking about store. After all how often product data changes?

Upvotes: 1

Marcin
Marcin

Reputation: 49866

Django does not natively generate javascript for you. The usual way is to import your javascript into your pages in your templates.

Upvotes: 0

Related Questions