The {% blocktrans %} template tag

The {% blocktrans %} template tag allows you to mark content that includes literals and variable content using placeholders. The following example shows you how to use the {% blocktrans %} tag, including a name variable in the content for translation:

{% blocktrans %}Hello {{ name }}!{% endblocktrans %}

You can use with to include template expressions such as accessing object attributes or applying template filters to variables. You always have to use placeholders for these. You cannot access expressions or object attributes inside the blocktrans block. The following example shows you how to use with to include an object attribute to which the capfirst filter is applied:

{% blocktrans with name=user.name|capfirst ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.