简体   繁体   English

Django和Ajax的动态模板

[英]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. 我在django工作已经有一段时间了,我真的很想更多地去做动态网页,而实际网页重装并不常见。

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. 我已经阅读了有关可用的各种流行的javascript框架的信息,并且我总是觉得我缺少这个难题的一部分,尤其是在模板方面。

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. 我的印象是,除了我的django模板之外,大量的模板逻辑将使它进入JS。 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. 对于一个非常基本的示例,假设我正在Django中为具有一定数量属性的输入字段编写一些模板代码。 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. 然后,我还用JS编写了一个内容,当我单击一个按钮时,将生成具有所有适当属性的相同类型的另一个输入字段。 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. 使用Django模板语言。 Pass the data from view to template dynamically. 将数据从视图动态传递到模板。

Read Django Template Language documentation. 阅读Django模板语言文档。

For JS : its better to write your js in home.html.... use {% include %} tag for other html 对于JS :最好在home.html中编写您的js。...对于其他html使用{%include%}标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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