[英]django ajax and non-ajax templates
i have a django template - one that is normally loaded via a standard get request. 我有一个Django模板-通常通过标准的get请求加载的模板。 However, i would also like to use this template for an ajax get.
但是,我也想使用此模板进行ajax获取。
I know i can use request.is_ajax to distinguish the call, and thus work out which page i should serve - what i don't know is how to avoid replication. 我知道我可以使用request.is_ajax区分呼叫,从而制定出我应该成为哪一页-我不知道是如何避免复制。
The problem is, the page extends a base htm file - one that has all the bells and whistles (you know, header, menu et al). 问题是,页面扩展了一个基本的htm文件-一个包含所有内容的文件(您知道标题,菜单等)。 I don't want this to appear in the ajax page though!
我不希望这个出现在ajax页面中! What i'd like there is for the page to appear, not extending base htm
我想让页面出现, 而不扩展基本htm
I can only think that perhaps i have two files - one that has just the contents (ajax version) and another that extends base htm, and somehow imports (not extends) the first file... 我只能认为也许我有两个文件-一个仅包含内容(ajax版本),另一个扩展基本htm,然后以某种方式导入(不扩展)第一个文件...
any ides how i'd do the above, or how i am meant to solve this generally? 任何想法我将如何做到以上几点,或者我一般是如何解决这个问题的?
In addition to @Daniel response, you can use yesno template filter if you have request
in context processors, eg 除了@Daniel响应之外,如果您在上下文处理器中有
request
,则可以使用yesno模板过滤器,例如
{% extends request.is_ajax|yesno:"base_ajax.html,base.html" %}
This line should be in every template you use for both page and ajax content. 此行应位于您用于页面和Ajax内容的每个模板中。
The value of extends
can be a variable. extends
的值可以是一个变量。 So you just define a base_ajax
html which omits the "bells and whistles", and in your view you send either base
or base_ajax
to the template context, depending. 因此,您只需要定义一个
base_ajax
html即可,该html省略了“风铃”,在您的视图中,您可以将base
或base_ajax
发送到模板上下文中,具体取决于。
Solution 1 : 解决方案1:
base.html: base.html:
{% block menu %}
menu goes here
{% endblock menu %}
non-ajax.html: non-ajax.html:
{% extends base.html %}
ajax.html: ajax.html:
{% extends base.html %}
{% block menu %}
{% end block %}
Solution 2: 解决方案2:
use same base.html 使用相同的base.html
ajax-and-non-ajax.html: ajax-and-non-ajax.html:
{% extends base.html %}
{% ifequal ajax %}
{% block menu %}
{% end block %}
{% endifequal %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.