Opened 3 years ago

Closed 3 years ago

Last modified 3 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: 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

Change History (5)

comment:1 Changed 3 years 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 3 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 3 years 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 3 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 urls.py. Here is an example:

urls.py

    (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!'),
        },

generic_delete.html

{% 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 }}
                </h3>
            </div>
            <div class="modal-body">
                <p class="text-error">
                    {{ message|interpolate:object }}
                </p>
                {% csrf_token %}
            </div>
            <div class="modal-footer">
                <a class="btn" href="{{ next }}">{% trans 'Close' %}</a>
                <input type="submit" class="btn btn-danger" value="{% trans 'Delete' %}">
            </div>
        </form>
    </div>
    <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 3 years ago by aalexgabi (next)

comment:5 Changed 3 years 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.

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