MVC 模型中 django 模板标签的用途是什么?
据我了解,模板标签是组织表示逻辑的一种方式,这是正确的吗?
我想要做的是加载/读取一堆模型对象,但根据过滤器将它们显示在模板的不同区域中。
第一个想法是实现一个标签 get_myobjects_by_filterx 但随后我必须为我拥有的每个过滤器实现它,并且我将通过在我希望它们显示的模板区域上调用该标签来使用它。
所以第二个想法是将 filterx 移动到标签本身中,因此调用将是 get_myobjects_by "X" (将过滤器作为字符串传递),然后在标签中过滤并返回。这是首选方式吗?如果我需要传递另一个对象而不仅仅是字符串怎么办?
我可以用过滤器代替吗?
我想要做的更详细一点:加载一种我的对象并按其某些字段对其进行过滤,具体取决于应用的过滤器,将其显示在模板的不同区域中。例如,我想要“如果按 X 过滤的对象显示 x 列中的所有对象,否则显示 y 列中的所有对象,依此类推”。
我如何用django解决这种问题?
谢谢。
It is my understanding that template tags are a way to organize presentation logic, is this correct?
What I want to do is load/read a bunch of my model objects, but show them in different areas of the template according to a filter.
The first thought was to implement a tag get_myobjects_by_filterx but then I have to implement it for each filter I have, and I would use it by calling the tag on the areas of my template where I want them to show up.
So the second thought was to move the filterx into the tag itself, so the call would be get_myobjects_by "X" (passing the filter as string) then in the tag filtering and returning. Is this the prefered way? What if I need to pass another object not just a string?
Could I use a filter instead?
Little more detailed what I want to do: Load a kind of my objects and filter it by some of its fields, depending on which filter is applied, show it in different areas of my template. For example I want "if objects filtered by X show all of those in the column x else in column y and so on".
How do I solve this kind of question with django?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我的理解,模板标签只不过是一些可以在模板内部使用的Python函数。因为 django 模板不是普通的 python 程序。
As far as my understanding goes, template tags are nothing but some python functions which can be used inside the templates. Because a django template is not your normal python program.