Django 通用模板
所以,通用视图非常酷,但我感兴趣的是通用模板。
例如,我可以给它一个对象,它会为我串起来。
或者如果我给它一个列表,它只会迭代对象并将它们作为 ul (或 tr,或它认为必要的其他任何内容)字符串。
对于大多数用途,你不需要这个。我只是为一个朋友快速拼凑了一些东西(如果你一定知道的话,一个酒吧股票应用程序),而且我不想编写模板。
So, Generic views are pretty cool, but what I'm interested in is something that's a generic template.
so for example, I can give it an object and it'll just tostring it for me.
or if I give it a list, it'll just iterate over the objects and tostring them as a ul (or tr, or whatever else it deems necessary).
for most uses you wouldn't need this. I just threw something together quickly for a friend (a bar stock app, if you must know), and I don't feel like writing templates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果有适用的 django 模型,您只需使用 django.contrib.admin 或 django.contrib.databrowse 即可。如果没有,那么您可以通过完全跳过 django 模板来进行管理。示例:
但是您当然想避免写那么多,因此我们可以使用纯文本和
pprint
模块来代替 html:编辑: 嗯...这似乎是视图装饰器应该处理的事情:
If there's a django model for it, you can just stick to
django.contrib.admin
ordjango.contrib.databrowse
. If not, then you might manage by skipping the django template altogether. example:But of course you wanted to avoid even writing that much, so instead of doing html, we can use plain text and the
pprint
module:edit: Hmm... this seems like something a view decorator should handle:
我不认为你会放弃编写模板,特别是如果你想格式化它,即使是轻微的格式化。
但是,您可以重复使用基本模板,例如,创建通用 object_list.html 和 object_detail.html,
它们基本上包含循环对象列表并呈现它的信息,并显示对象详细信息。如果需要,您可以在整个应用程序中使用这些“通用”模板。
I don't see you getting away from writing templates, especially if you would want to format it, even slightly.
However you can re-use basic templates, for e.g, create a generic object_list.html and object_detail.html
that will basically contain the information to loop over the object list and present it, and show the object detail. You could use these "Generic" templates across the entire app if need be.