Google App Engine(Python) - 站点碎片 - 复合视图 [英] Google App Engine(Python) - site fragments - composite view

查看:86
本文介绍了Google App Engine(Python) - 站点碎片 - 复合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法构建一个站点的三个或四个部分(三个或四个html模板),然后在GAE python中将它们中的一些或全部组合在一起?我知道我可以加载和渲染一个特定的html django模板,但是我想为不同文件中的网站的不同部分构建模板,然后根据情况将它们组合在一起。



一个很好的例子是,我希望在我的大多数Web应用程序页面中都使用相同的菜单,页眉,页脚,但是我想切换内容的特定部分。
所以我想有一个文件和模板处理让讲课和另一个处理学生,所以网站(main.html)的一般外观保持不变,但我显示和处理的方式关于学生或班级的信息是完全不同的。我基本上想把一堆页面特定的html植入通用模板。
感谢您的任何帮助。 :)



我不知道我正在寻找什么是正确的技术术语(我试过搜索)。 =>我认为他们称之为Zend框架中的复合视图或站点片段。

解决方案

您应该在Django中使用模板继承。查看本教程,以开始。



编辑官方 Django模板继承的图书部分也展示了不同的片段,例如一个页脚或一个导航栏可以存储在不同的模板文件中,并通过包含和继承进行组合。



此网站显示了一个模板如何从另一个模板继承,就像网站部分模板扩展基本布局模板一样,使用代码,例如

  {%extendsbase.html%} 

它还显示了如何使用模板包含,例如,添加不同的部分到较大的模板,如拼图中的碎片。例如,导航片段可以添加到布局文件中,其中包含短语

  {%includenav.html%} 

如@Nick Johnson的评论所述: extends 更紧凑,可以不需要使用多个文件片段。 包含,如果扩展失败。



编辑另请参阅我的如何将大型HTML文件剪切成多个HTML文件的问题的答案


Is there a way to build three or four parts of a site (three or four html templates) and then render some of them or all of them together in GAE python? I know I can load and render one specific html django template but I want to build templates for different parts of the site in different files and then compose them together depending on the situation.

A good example would be that I want pretty much the same menu, header, footer in most of my web application pages but I want to switch a specific part of the content. So I would like to have one file and template that deals with lets say classes and another that deals with students, so the general look of the site (main.html) stays the same but the way I display and handle the information about students or classes is completely different. I basically want to plant a bunch of page specific html into a generic template. Thanks for any help on this. :)

I am not sure what is the correct technical term for what I'm looking for(I tried searching). => I think they call it composite view or site fragments in the Zend framework.

解决方案

You should use template inheritance in Django. Have a look at this tutorial for a start.

EDIT The official Django Book section on Template Inheritance also demonstrates how different 'fragments' e.g. a footer, or a nav bar, may be stored in different template files and brought together via inclusion and inheritance.

This site shows how one template can inherit from another, as when a site section template extends a basic layout template, with the code, for example,

{% extends "base.html" %}

It also shows how using template inclusion one may, for example, add different pieces to a larger template like pieces in a puzzle. For example, a navigation fragment may be added to a layout file with the phrase

{% include "nav.html" %}

As noted in the comments by @Nick Johnson: extends is more compact and can make the use of multiple file fragments unnecessary. Only include as last resort, if extends fails you.

EDIT See also my answer to a question on "How to cut large HTML file into multiple HTML files"

这篇关于Google App Engine(Python) - 站点碎片 - 复合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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