一个视图中的多个 Django 应用程序
我想我有一个关于更好的代码组织的简单问题。
假设我有多个应用程序,它们也实现了这些应用程序在表示层上的表示方式。
我想了解如果我需要在一页上呈现多个应用程序而不使用框架,如何组织代码?
简单的例子:假设我有两个应用程序(app1 和 app2)都实现了它们对应的模型和视图。现在我需要我的索引页面包含这两个视图的演示。我如何实现仍然使用应用程序视图而不是直接转到其模型的通用视图? 我希望我的应用程序仍然控制其视图。
谢谢
I guess i have a simple question about better code organization.
Say i have multiple apps that also implement how these apps should be represented on presentation layer.
I am trying to understand how to organize the code if i need to present multiple apps on one page without using frames of course?
Quick example: say I have two apps (app1 and app2) both implmeneting their coresponding model and views. Now i need my index page to contain presentation of these two views. How can i implement the generic view that still utilizes the app views instead of going to their models directly?
I would prefer my app to control its view still.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可以使用 render_to_string 快捷方式
因此,在应用程序视图中,您输入:
然后在索引视图中输入如下内容:
PS:另外,在我写完此内容之后,它看起来不太好(在我的脑海中看起来更好:))。
I think you may use render_to_string shortcut
Thus in app views you put:
and then in index view something like this:
PS: Also after i wrote this it doesn't look very nice (it looked better in my head :) ).
好问题,我告诉你我是怎么做到的。
我使用(特定于应用程序的)自定义模板标签。这些模板标签将额外的内容放入上下文中,您可以在模板中使用这些内容。您可以从文档中阅读有关自定义模板标签的更多信息。
有一本好书,Practical django Projects 2nd Edition,我认为这本书有点过时了,但是给了你一个对项目组织的深刻见解。
Great question, I tell you how I did it.
I used (app specific) custom template tags. These template tags put extra stuff into the context, what you can use in your templates. You can read more from the docs about custom template tags.
There's a good book, Practical django Projects 2nd edition, which is a bit outdated I think, but gives you a great insight to project organization.