Code

Opened 4 years ago

Closed 4 years ago

#14369 closed (wontfix)

TEMPLATE_STRING_IF_INVALID settable per template rendering

Reported by: vanschelven Owned by: nobody
Component: Template system Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

It would be nice if TEMPLATE_STRING_IF_INVALID is settable per template rendering.

I have created a version of this here:
http://bitbucket.org/vanschelven/django/changeset/7cfe30a771b7

In this particular prototype the TEMPLATE_STRING_IF_INVALID is looked up in the context, as well as in the settings (in that order).
A greater reworking of the templating system would allow for
template.render(......, string_if_invalid="YOUR_STRING %s")

In some cases you want to be absolutely sure that whatever you tell the template to render either shows up, or generates an error.
This can also be used in combination with Exceptions on rendering by using a tool like this:

http://bitbucket.org/vanschelven/django_invalid_template_exception/src/2c5772c3f21d/invalid_template_exception/__init__.py

In some cases (notably the admin) it is unfortunately currently impossible to use the TEMPLATE_STRING_IF_INVALID at all.
Having a per-template approach allows for more flexibility.

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by mariarchi

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

I think it's arguable whether this feature is a good idea. I would recommend raising a discussion on django-developers

comment:2 Changed 4 years ago by lukeplant

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

I don't think this is a good idea. TEMPLATE_STRING_IF_INVALID is clearly marked as being for debugging purposes only, and that is very unlikely to change. Re-usable apps often make the same assumption that the admin does.

The workaround for your use case is to make a unicode subclass like this:

class TemplateInvalidString(unicode):
    pass

TEMPLATE_STRING_IF_INVALID = TemplateInvalidString(u"")

You can then implement some template filter that detects TemplateInvalidString and raises an error or something. This allows for a much more fine-grained approach - you put a filter just on the variables that must be present, and not others.

Add Comment

Modify Ticket

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


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

 
Note: See TracTickets for help on using tickets.