Opened 5 years ago

Closed 5 years ago

#30539 closed Bug (worksforme)

Django - “with / as” to fill block is not working.

Reported by: Jay Day Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords: templates, blocks, with
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am trying to not repeat myself in Django, but use the same content for 2 blocks. The content is static so I would rather have in in my templates than sending them through views.

This was the solution I found :

     {% with "My title" as title %}
         {% block TitleOne %}{{ title }}{% endblock TitleOne %}
         {% block TitleTwo %}{{ title }}{% endblock TitleTwo %} 
{% endwith %}

In a templates that extends a second one that uses the blocks TitleOne and TitleTwo .

But it does not work. If I write it like :

{% block TitleOne %}"My title"{% endblock TitleOne %}
{% block TitleTwo %}"My title"{% endblock TitleTwo %}


It works perfectly. But of course it s not DRY. If I write it like :

     {% with "My title" as title %}
         {% block TitleOne %}"My title"{% endblock TitleOne %}
         {% block TitleTwo %}{{ title }}{% endblock TitleTwo %}
{% endwith %}

Only the 1st one displays right. But not DRY as well.

Tried another way suggested in Django's docs : https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#with

{% with title="My title" %}
             {% block TitleOne %}"My title"{% endblock TitleOne %}
             {% block TitleTwo %}{{ title }}{% endblock TitleTwo %}
    {% endwith %}

Only the 1st one displayed too..

(I am using Django 2.2.1, Python 3.7.3)

Change History (1)

comment:1 Changed 5 years ago by Mariusz Felisiak

Resolution: worksforme
Status: newclosed
Summary: Django - “with / as” to fill block is not workingDjango - “with / as” to fill block is not working.
UI/UX: unset
Version: 2.2master

Thanks for the report, however I cannot reproduce your issue. In all described cases everything works for me.

Note: See TracTickets for help on using tickets.
Back to Top