Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#19735 closed New feature (wontfix)

Sometimes string interpolation is needed in templates

Reported by: aalexgabi Owned by: nobody
Component: Template system Version:
Severity: Normal Keywords: filter, interpolation
Cc: Marc Tamlyn Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Change History (5)

comment:1 Changed 4 years ago by Marc Tamlyn

Needs documentation: set
Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedDesign decision needed
Version: 1.4

comment:2 Changed 4 years ago by anonymous

(Reading the Stack Overflow question) I don't understand what's the relationship with translation.

If you need to interpolate and translate you can simply use the blocktrans tag in the i18n template library.

comment:3 Changed 4 years ago by Carl Meyer

Resolution: wontfix
Status: newclosed

I don't think this is nearly a common enough need to warrant adding a new built-in filter to Django. There's a reason Django lets you write your own filters and tags when you need them, and this one is easy enough to write for those who want it.

comment:4 Changed 4 years ago by aalexgabi

If the view and the template are generic it is not possible to use blocktrans. Specific parameters for generic views are passed in Here is an example:

    (r'^media/delete/(?P<object_id>[0-9]+)/$', views.generic_delete, {
        'model': models.Media,
        'template_name': 'openservices/partials/generic_delete.html',
        'extra_context': {
            'page_title': _('Delete media'),
            'message': _('Do you want to remove media %s? Media will be lost!'),


{% block content %}
    <div class="modal in" tabindex="-1" role="dialog"
         aria-labelledby="formHeader" aria-hidden="true" data-keyboard="false" data-backdrop="static"
         style="display: block">
        <form class="form-horizontal" method="post">
            <div class="modal-header">
                <a class="close" href="{{ next }}">×</a>
                <h3 id="formHeader">
                    {{ page_title }}
            <div class="modal-body">
                <p class="text-error">
                    {{ message|interpolate:object }}
                {% csrf_token %}
            <div class="modal-footer">
                <a class="btn" href="{{ next }}">{% trans 'Close' %}</a>
                <input type="submit" class="btn btn-danger" value="{% trans 'Delete' %}">
    <a class="modal-backdrop in" href="{{ next }}"></a>
{% endblock %}

The same template and view is used for multiple models. For each model there is a different confirmation message. Blocktrans cannot be used because it requires hard coding the string that needs to be translated in the template.

Version 0, edited 4 years ago by aalexgabi (next)

comment:5 Changed 4 years ago by Aymeric Augustin

URLconfs aren't supposed to contain your entire application.

In this case it's much more reasonable to subclass the generic view and override get_context_data.

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