Opened 15 months ago

Closed 15 months ago

Last modified 15 months 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: mjtamlyn Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Attachments (0)

Change History (5)

comment:1 Changed 15 months ago by mjtamlyn

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Design decision needed
  • Version 1.4 deleted

comment:2 Changed 15 months 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 15 months ago by carljm

  • Resolution set to wontfix
  • Status changed from new to closed

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 15 months 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 15 months ago by aalexgabi (next)

comment:5 Changed 15 months ago by aaugustin

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.