Django:使{%block“div” %}条件与条件{%extends%} [英] Django: making {% block "div" %} conditional with a conditional {% extends %}

查看:99
本文介绍了Django:使{%block“div” %}条件与条件{%extends%}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在AJAX和regualr HTTP调用之间分享一个模板,唯一的区别是一个模板需要扩展base.html html,而另一个模板不需要。



我可以使用

  {%extends request.is_ajax | yesno :app / base_ajax.html,app / base.html%} 

要动态决定何时扩展,但我还需要包含 {%block'some_div'%} {%endbock%} 标签来告诉渲染器将内容放在哪里。 ajax调用需要这些标签被忽略,因为jQuery告诉它将内容放在哪里,使用 $('somediv')。html(response)

任何关于如何在不是ajax调用时动态包含这些块标签的想法?



I我已经提到了这个问题来弄清楚:



任何使{%extends'...'%}有条件的方式 - Django



尝试通过{%if%}进行工作:



  {%extends request.is_ajax | yesno:,stamped / home.html%} 
{%if request.is_ajax == False%}
{%block results %}
{%endif%}
{%load stamped_custom_tags%}

...
内容
...
{%如果request.is_ajax == False%}
{%endblock%}
{%endif%}

但是当解析器运行到{%endif%}

解决方案

我的解决方案:

  {%extends x | yesno:stamped / blank.html,stamped / home.html%} 

其中 blank.html 包含:

  {%block results%} {%endblock%} 

<! - 允许更正共享呈现
与ajax帖子和正常django渲染 - >

home.html 是我的标准应用页面带有结果块来扩展。


I would like to share a template between AJAX and regualr HTTP calls, the only difference is that one template needs to extend base.html html, while the other dose not.

I can use

{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}

To dynamically decide when to extend, but I also need to include {% block 'some_div' %}{% endbock %} tags to tell the renderer where to put my content. The ajax call needs those tags to be left out because jQuery tells it where to put the content with $('somediv').html(response).

Any idea on how to dynamically include those block tags when its not an ajax call?

I've been referencing this question to figure it out:

Any way to make {% extends '...' %} conditional? - Django

Attempt to make it work through an {% if %}:

{% extends request.is_ajax|yesno:",stamped/home.html" %}
{% if request.is_ajax == False%}
{% block results %}
{% endif %}
{% load stamped_custom_tags %}

...
Content
...
{% if request.is_ajax == False%}
{% endblock %}
{% endif %}

but this fails when parser runs into the {% endif %}

解决方案

My Solution:

{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}

Where blank.html contains:

{% block results %}{% endblock %}

<!-- to allow for corrected shared rendering 
    with ajax posts and normal django rendering -->

and home.html is my standard app page with a results block to extend.

这篇关于Django:使{%block“div” %}条件与条件{%extends%}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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