Django Master-Detail View插件 [英] Django Master-Detail View Plugins

查看:275
本文介绍了Django Master-Detail View插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个Django应用程序,应用程序国家,应用程序社交和应用程序财务



国家是一个主导航应用程序。它列出索引视图中的所有国家,并在其详细信息视图上显示每个国家的详细信息。



每个国家的详细信息包括他们的社交细节(来自社交应用程序)和他们的财务详细信息(来自财务应用程序)。



社会和财务都有详细的视图(每个国家/地区)



有一种优雅的方式来将这些子细节视图插入国家/地区提供的主详细信息视图中?所以对于每个国家的细节页面,我会看到2个标签显示该国的社会和财务细节。

解决方案

2常见解决方案我用于这个问题:



部分模板:

创建一个用于渲染社会和财务除了正在处理的对象(并使用对象函数或模板标签来呈现它)之外,它不需要视图中的东西。
那么你可以很容易地{%include%}它(并且首先设置所需的变量)。

这个局部视图不会渲染一个完整的HTML页面,而只能使用一个DIV或其他一些HTML要使用的元素如果您还需要仅社交页面,则可以创建一个页面来呈现标题,然后包含部分模板。您可以使用类似_template.html的常规模板和template.html作为常规模板。



AJAX:

使您的社交和财务视图在XMLHTTPRequest(request.is_ajax())中被调用。如果是,它们只返回一个DIV元素,而不是所有的HTML。这样,您的主页面可以无需渲染,并且即时添加该内容。

AJAX方式有以下几个优点:您不会在与整个页面相同的请求中呈现插件视图,因此如果您有许多这些插件视图,主页面将加载更快,并且您可以使用一个智能的JavaScript只选择相关的插件视图来要求。

此外,您可以使用普通视图生成模板中需要的数据(您在Partial Templates方法中无法真正做到这一点)。


Let's say I have 3 django apps, app Country, app Social and app Financial.

Country is a 'master navigation' app. It lists all the countries in a 'index' view and shows details for each country on its 'details' view.

Each country's details include their Social details (from the social app) and their Financial details (from the financial app).

Social and Financial both have a detail view (for each country)

Is there an elegant way to 'plug' in those sub-detail views into the master detail view provided by Countries? So for each country detail page I would see 2 tabs showing the social and the financial details for that country.

解决方案

2 common solution I use for this problem:

Partial Templates:
Create a template for rendering "social" and "financial" that does not need stuff from the view, other than the object it is working on (and uses the objects functions or template tags to render it). then you can easily {% include %} it (and set the needed variable first).
This partial view does not render a full HTML page, but only a single DIV or some other HTML element you wish to use. If you also need a "social-only" page, you can create a page that renders the header and then includes the partial template. You can use a convention like _template.html for the partial template, and template.html for the regular template.

AJAX:
Make your "social" and "financial" views aware of being called in XMLHTTPRequest (request.is_ajax()). If they are, they return only a DIV element, without all the HTML around it. This way your master page can render without it, and add that content on the fly.
The AJAX way has several advantages: you don't render the plugin views on the same request as the whole page, so if you have many of these plugin views, the master page will load faster, and you can have a smart javascript choose only the relevant plugin views to ask for.
Also, you can use the normal view to generate data you need in the template (which you can't really do in the Partial Templates method).

这篇关于Django Master-Detail View插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆