python - Pass value from one Django template to other - Stack Overflow

I want to build a Django template hierarchy like so:root.html|_ root-dashboard.html|_ root-regular.html

I want to build a Django template hierarchy like so:

  root.html
  |_ root-dashboard.html
  |_ root-regular.html

root.html shall have an if statement:

{% if style == "dashboard" %}
  {# render some elements in a certain way #}
{% else %}
  {# render those elements in a different way #}
{% endif %}

And root-dashboard.html and root-regular.html should individually extend root.html by setting style:

# root-dashboard.html
{% extend 'root.html' with style='dashboard'%}

# root-regular.html
{% extend 'root.html' with style='regular'%}

(with above is not an actual valid syntax, its just something similar I want)

And a view can use either root-dashboard.html or root-regular.html to show the content in one style or the other.

How do I achieve this without the view having to set the style context?

I want to build a Django template hierarchy like so:

  root.html
  |_ root-dashboard.html
  |_ root-regular.html

root.html shall have an if statement:

{% if style == "dashboard" %}
  {# render some elements in a certain way #}
{% else %}
  {# render those elements in a different way #}
{% endif %}

And root-dashboard.html and root-regular.html should individually extend root.html by setting style:

# root-dashboard.html
{% extend 'root.html' with style='dashboard'%}

# root-regular.html
{% extend 'root.html' with style='regular'%}

(with above is not an actual valid syntax, its just something similar I want)

And a view can use either root-dashboard.html or root-regular.html to show the content in one style or the other.

How do I achieve this without the view having to set the style context?

Share Improve this question asked Mar 8 at 11:02 fishfinfishfin 2931 silver badge8 bronze badges 1
  • The {% if .. %} probably is not a good idea: just like in OO programming where you use dynamic binding over if, it is better to use a block. – willeM_ Van Onsem Commented Mar 8 at 11:05
Add a comment  | 

1 Answer 1

Reset to default 1

Define a {% block … %} template tag [Django-doc] instead.

In root.html, you don't use an if, but:

{% block render_item %}
{# render those elements in a different way #}
{% endblock %}

then in your root-dashboard.html, you use:

# root-dashboard.html
{% extend 'root.html' %}
{% block render_item %}
{# render some elements in a certain way #}
{% endblock %}

The idea is similar to the dynamic binding concept [wiki] in object-oriented programming, and is usually better than using if conditions: the latter is not extensible, and thus limits later modifications.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744897767a4599798.html

相关推荐

  • python - Pass value from one Django template to other - Stack Overflow

    I want to build a Django template hierarchy like so:root.html|_ root-dashboard.html|_ root-regular.html

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信