I want to include things like twitter status, or delicious tags, in my django templates.
我希望在我的django模板中包含twitter状态或美味标签等内容。
These things are dynamic, yet regular. How would this be done?
这些东西是动态的,但却很规律。怎么做?
3 个解决方案
#1
There are a number of ways to handle this, so you can choose a method that best matches your own personal style or requirements:
有很多方法可以解决这个问题,因此您可以选择最符合您个人风格或要求的方法:
-
Template context variable: as answered by Alex you can put your content into a context variable that is included in the context of every template created by every view. Django even provides a mechanism for doing this automatically, called a context processor. Pros: very straightforward. Cons: won't dynamically refresh new content on client browsers.
模板上下文变量:正如Alex所回答的那样,您可以将内容放入上下文变量中,该变量包含在每个视图创建的每个模板的上下文中。 Django甚至提供了一种自动执行此操作的机制,称为上下文处理器。优点:非常简单。缺点:不会动态刷新客户端浏览器上的新内容。
-
AJAX dynamic loading: as mentioned by Alex and Dave you can dynamically load your content using AJAX methods. As an example using jQuery, you would put a placeholder in your template something like
<div id="twitterfeed"></div>
and then in a javascript block in your template put$("#twitterfeed").load("{% url twitterfeed %}");
wheretwitterfeed
is a url so named in your urls.py. Pros: will dynamically update browsers. Cons: can be tricky if you don't know Javascript.AJAX动态加载:如Alex和Dave所述,您可以使用AJAX方法动态加载内容。作为使用jQuery的示例,您可以在模板中放置一个类似
的占位符,然后在模板的javascript块中放置$(“#twitterfeed”)。load(“{ %url twitterfeed%}“);其中twitterfeed是一个在你的urls.py中命名的url。优点:将动态更新浏览器。缺点:如果您不了解Javascript,可能会很棘手。 -
Inclusion tag: Django provides a type of template tag called an inclusion tag, which is basically a custom template tag that can render dynamic content. In a way it's similar to a context variable, except your code to generate the content will only be called when you use the custom template tag in your template instead of being called for every view. Another benefit is the content is generated from a template of its own. You could do this with a normal context variable of course, but it's not as clean (IMHO) as using an inclusion tag. Pros: very straightforward, clean. Cons: won't dynamically refresh new content on client browsers.
包含标记:Django提供了一种称为包含标记的模板标记,它基本上是一个可以呈现动态内容的自定义模板标记。在某种程度上,它类似于上下文变量,除了生成内容的代码只有在模板中使用自定义模板标记而不是为每个视图调用时才会被调用。另一个好处是内容是从它自己的模板生成的。当然,您可以使用普通的上下文变量执行此操作,但它不像使用包含标记那样干净(IMHO)。优点:非常简单,干净。缺点:不会动态刷新客户端浏览器上的新内容。
#2
The simplest approach is to use {{ mycontent }}
in your template (where you want the dynamically generated content to appear) and put the correspondence between mycontent
and its value in the context you use to render the template -- i.e., the most fundamental part of django's templating.
最简单的方法是在模板中使用{{mycontent}}(您希望动态生成的内容出现在其中),并将mycontent及其值之间的对应关系放在用于呈现模板的上下文中 - 即最基本的django的模板的一部分。
If what you mean is that you want Ajax support whereby Javascript on the page continuously refreshes such content according to what the server wants it to be at any given time, I suggest looking into dojango, the Dojo/Django integration project -- it's not yet as fully mature as each of Dojo and Django are on their own (not version 0.4 yet), but it is already usable and useful.
如果你的意思是你想要Ajax支持,那么页面上的Javascript会根据服务器在任何给定时间想要的内容不断刷新这些内容,我建议调查dojango,Dojo / Django集成项目 - 它还没有因为每个Dojo和Django都是完全成熟的(不是版本0.4),但它已经可用且有用。
#3
A common technique is to leave a placeholder div in the generated content, then fill the div in on the client side via an AJAX call from Javascript that you include in the page.
一种常见的技术是在生成的内容中留下占位符div,然后通过您在页面中包含的Javascript的AJAX调用填充客户端的div。
That gives you the benefit of having a cacheable (fast loading) primary page, with separate dynamic bits. Depending on how live you want the dynamic bits, you can can even cache them for shorter durations.
这为您提供了具有可缓存(快速加载)主页面的好处,具有单独的动态位。根据您想要动态位的实时情况,您甚至可以将它们缓存为更短的持续时间。
#1
There are a number of ways to handle this, so you can choose a method that best matches your own personal style or requirements:
有很多方法可以解决这个问题,因此您可以选择最符合您个人风格或要求的方法:
-
Template context variable: as answered by Alex you can put your content into a context variable that is included in the context of every template created by every view. Django even provides a mechanism for doing this automatically, called a context processor. Pros: very straightforward. Cons: won't dynamically refresh new content on client browsers.
模板上下文变量:正如Alex所回答的那样,您可以将内容放入上下文变量中,该变量包含在每个视图创建的每个模板的上下文中。 Django甚至提供了一种自动执行此操作的机制,称为上下文处理器。优点:非常简单。缺点:不会动态刷新客户端浏览器上的新内容。
-
AJAX dynamic loading: as mentioned by Alex and Dave you can dynamically load your content using AJAX methods. As an example using jQuery, you would put a placeholder in your template something like
<div id="twitterfeed"></div>
and then in a javascript block in your template put$("#twitterfeed").load("{% url twitterfeed %}");
wheretwitterfeed
is a url so named in your urls.py. Pros: will dynamically update browsers. Cons: can be tricky if you don't know Javascript.AJAX动态加载:如Alex和Dave所述,您可以使用AJAX方法动态加载内容。作为使用jQuery的示例,您可以在模板中放置一个类似
的占位符,然后在模板的javascript块中放置$(“#twitterfeed”)。load(“{ %url twitterfeed%}“);其中twitterfeed是一个在你的urls.py中命名的url。优点:将动态更新浏览器。缺点:如果您不了解Javascript,可能会很棘手。 -
Inclusion tag: Django provides a type of template tag called an inclusion tag, which is basically a custom template tag that can render dynamic content. In a way it's similar to a context variable, except your code to generate the content will only be called when you use the custom template tag in your template instead of being called for every view. Another benefit is the content is generated from a template of its own. You could do this with a normal context variable of course, but it's not as clean (IMHO) as using an inclusion tag. Pros: very straightforward, clean. Cons: won't dynamically refresh new content on client browsers.
包含标记:Django提供了一种称为包含标记的模板标记,它基本上是一个可以呈现动态内容的自定义模板标记。在某种程度上,它类似于上下文变量,除了生成内容的代码只有在模板中使用自定义模板标记而不是为每个视图调用时才会被调用。另一个好处是内容是从它自己的模板生成的。当然,您可以使用普通的上下文变量执行此操作,但它不像使用包含标记那样干净(IMHO)。优点:非常简单,干净。缺点:不会动态刷新客户端浏览器上的新内容。
#2
The simplest approach is to use {{ mycontent }}
in your template (where you want the dynamically generated content to appear) and put the correspondence between mycontent
and its value in the context you use to render the template -- i.e., the most fundamental part of django's templating.
最简单的方法是在模板中使用{{mycontent}}(您希望动态生成的内容出现在其中),并将mycontent及其值之间的对应关系放在用于呈现模板的上下文中 - 即最基本的django的模板的一部分。
If what you mean is that you want Ajax support whereby Javascript on the page continuously refreshes such content according to what the server wants it to be at any given time, I suggest looking into dojango, the Dojo/Django integration project -- it's not yet as fully mature as each of Dojo and Django are on their own (not version 0.4 yet), but it is already usable and useful.
如果你的意思是你想要Ajax支持,那么页面上的Javascript会根据服务器在任何给定时间想要的内容不断刷新这些内容,我建议调查dojango,Dojo / Django集成项目 - 它还没有因为每个Dojo和Django都是完全成熟的(不是版本0.4),但它已经可用且有用。
#3
A common technique is to leave a placeholder div in the generated content, then fill the div in on the client side via an AJAX call from Javascript that you include in the page.
一种常见的技术是在生成的内容中留下占位符div,然后通过您在页面中包含的Javascript的AJAX调用填充客户端的div。
That gives you the benefit of having a cacheable (fast loading) primary page, with separate dynamic bits. Depending on how live you want the dynamic bits, you can can even cache them for shorter durations.
这为您提供了具有可缓存(快速加载)主页面的好处,具有单独的动态位。根据您想要动态位的实时情况,您甚至可以将它们缓存为更短的持续时间。