Django - 通用视图 - Object_list - 有什么好处?
我想使用 object_list通用视图。但我不知道它除了分页之外还给我带来什么好处?看来我还需要自己写模板?
这比写我自己的观点快多少?我错过了什么吗?
如果它值得使用并且我确实必须编写自己的模板,那么模板中应该包含什么?我找不到任何例子。
I want to use the object_list generic view. But I can't tell what benefit it gives me besides pagination? It seems like I still need to write my own template?
How is this faster than writing my own view? Am I missing something?
If it is worth using and I do have to write my own template, what is supposed to be in the template? I can't find any examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有多个模型,则可以节省复制类似视图和模板的时间。假设您编写了一个包含 15 个模型的应用程序,您仍然只需要 1 个通用视图和 1 个模板来显示所有模型。
If you have more than one model, it saves you some time duplicating a similar view and template. Say you write an app with 15 models, you still only need 1 generic view and 1 template to show all of them.
您的想法是正确的,基于函数的通用视图并不能为您节省太多时间。一旦您需要一点点自定义,您最终就会自己编写视图。您还应该注意,它们在 Django 1.3 中已弃用,并且有一个 迁移指南。
另一方面,新的基于类的通用视图 1.3 非常方便。您可以编写相同的列表视图逻辑并换出响应混合来呈现 Excel 电子表格或返回 JSON,而不是呈现模板。
You are right in thinking that the function-based generic views do not save you very much. As soon as you need just a little bit of customization you end up writing the view yourself. You should also note that they deprecated in Django 1.3 and there is a migration guide.
On the other hand the new class-based generic views in 1.3 are very handy. You can write the same list view logic and swap out a response mix-in to render an Excel spreadsheet or return JSON instead of rendering a template.