简体   繁体   中英

Dynamic templates with Django and Ajax

My question is a bit generic in that my problem is a broad one. I have been working with django for some time, and I really want to move more into doing very dynamic web pages where actual page reloads aren't common.

I have read about the different popular javascript frameworks available, and I always feel like I am missing part of the puzzle, particularly in templating.

What are some of the best practices for keeping my templating code as non redundant as possible. I get the impression that a lot of templating logic will make it's way into the JS in addition to my django templates. I want to avoid situations where I am writing templating code in two different places.

For a very basic example, let's say I am writing some template code inside Django for an input field that has a set number of attributes. I have then also written in JS that when I click on a button, another input field of the same type is generated with all the appropriate attributes. In practice this could be a form that takes an arbitrary amount of e-mail addresses. The problem I see is that when I want to change something about that input field, I need to do it in two places.

Is there a particular development paradigm or work flow that I am unaware of? How are issues like this generally avoided?

Recommendations on frameworks would be amazing too!

as you mentioned above:

Use Django Template language. Pass the data from view to template dynamically.

Read Django Template Language documentation.

For JS : its better to write your js in home.html.... use {% include %} tag for other html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM