Package: django-shouty-templates
Author:Keryn KnightVersion:0.2.0BriefThis app applies a monkeypatch which forces Django’s template language to errorvery loudlyabout variables which areusedin a template butdon’t existin the context.RationaleGiven a template like this:<html><head></head>
<body>
{% if chef.can_add_cakes %}
    <label class="alert alert-{{ chef.is_cake_chef|yesno:"success,danger,default" }}
{% endif %}everything works fine, until you refactor and any of the following happens:chefis no longer the name of the variable.can_add_cakesis refactored to be calledcan_add_pastriesis_cake_chefis renamedis_pastry_kingIf those happen, the template will either silently display nothing, or will
display the label incorrectly. This monkeypatch attempts to fix that.Specifically:chefwill raise an exception if the variable were calledsous_chefchef.can_add_cakeswill raise an exception ifcan_add_cakeswas no longer a valid attribute/property/method ofchefchef.is_cake_chefwill raise an exception for the same reasons.Thus you can refactor somewhat more freely, knowing that if the template renders
it’s OK. It ain’t compile time safety, but it’s better than silently swallowing
errors because you forgot something!The exception itself would look something like:Token 'chef' of 'chef.can_add_cakes' in template 'my/cool/template.html' does not resolve.
Possibly you meant to use 'sous_chef'.
Silence this occurance only by adding 'chef.can_add_cakes': ['my/cool/template.html'] to the settings.SHOUTY_VARIABLE_BLACKLIST dictionary.
Silence this globally by adding 'chef.can_add_cakes': ['*'] to the settings.SHOUTY_VARIABLE_BLACKLIST dictionary.SetupThis package is available onPyPIand can be installed using pip or whatever like so:pip install django-shouty-templates==0.2.0Then addshouty.Shoutorshoutyto yoursettings.INSTALLED_APPSOptional configurationA list of values which may be set in your project’s settings module:settings.SHOUTY_VARIABLESMay beTrue|Falseand determines if the exception is raised when trying to
use a variable which doesn’t exist.Defaults toTrue.settings.SHOUTY_URLSMay beTrue|Falseand determines if an exception is raised when
doing{% url 'myurl' as my_var %}andmyurldoesn’t actually resolve to a view.Defaults toTrue.settings.SHOUTY_VARIABLE_BLACKLISTUseful for if you are trying to fix up an existing project, or ignore problems
in third-party templates.Expects adictofstrkeys and a sequence (eg:tupleorlist) of templates in which to ignore it:SHOUTY_VARIABLE_BLACKLIST = {
    "chef.can_add_cakes": ("*",),
    "my_sometimes_set_variable": ["admin/custom_view.html", "admin/custom_view_detail.html"],
    "random_in_memory_template": ["<unknown source>"],
    "*": ["admin/login.html", "<unknown source>"],
}Of special note is the use of*, which has a more magical meaning.Using"key":["*"]would silence errors about the variable namedkeyinall templatesUsing"*":["path/to/template.html"]would silenceallvariable errors inthat specific templateonly (seeGitHub issue 6)And also the far less frequently useful<unknown source>ordjango.template.base.UNKNOWN_SOURCEwhich is essentially usually forTemplateinstances
not loaded from afile on disksettings.SHOUTY_URL_BLACKLISTAtupleof2-tupleto prevent certain URLs and their output variables f
rom shouting at you loudly. Useful for existing projects or third-party apps which are less strict.By way of example,{% url "myurl" as my_var %}may be suppressed with:SHOUTY_URL_BLACKLIST = (
    ('myurl', 'my_var'),
)which would still let{% url "myurl" as "my_other_var" %}raise an exception.Default configurationThere’s a hard-coded blacklist of variables and URLs to make sure the Django admin (+ admindocs),
django-debug-toolbar, django-pipeline, django-admin-honeypot, djangorestframework, etc all work.if/elif/else testingWhen an{% if x %}statement is seen in the template, all conditions are checked
to ensure the context will always resolve correctly. Additionally, if you use{% if %}and{% elif %}together anddon’thave an{% else %}it’ll raise an error
to remind you to handle that case, even if handling it is just to output nothing.TestsJust runpython3-mshoutyand hope for the best. I usually do.The licenseIt’sFreeBSD. There’s should be aLICENSEfile in the root of the repository, and in any archives.Copyright (c) 2019, Keryn Knight
All rights reserved.Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Change history for django-shouty-templates0.2.0Bugfix: Updates to ostensibly support Django 4.0+Feature: Error loudly when an{% if ...%}...{%elif %}statement is present without an{% else %}Feature: Allow for silencingallmissing variables within a single given template (or set of templates)Bugfix: Attempt to fix parsing of where a missing variable occurs within things like{% blocktrans with filter_title=title %}Bugfix: when therender_contextdoesn’t have a templateorit’sNoneit’s now properly skipped.0.1.6Feature: Ignore variables which prevented much of REST Framework’s browsable API from working.Feature: Ignore variables which would cause thedjango-admin-honeypotpackage’s fake login screen to error.Feature: Ignore variables which might causedjango-pipelinerendering of<link>and<script>to error.Bugfix: Improve per-template silencing viaSHOUTY_VARIABLE_BLACKLISTBugfix: Attempt to have fewer false positives when highlighting the location in the template of the exception.0.1.5Feature: system check to validate anySHOUTY_VARIABLE_BLACKLISTin your settings.Feature: Attempt to highlight the exception location in any subtemplate, better than Django currently does.Feature: Update exception message with any potential typo candidates.Feature: Silencing missing variables on a per-template basis.Feature: Deep inspection of{% if ... %} {% elif ... %}nodes for any which are variables which don’t exist.Bugfix: Patch methods hidden from the debug error page should now be consistently hidden always, rather than dependant on hitting the expected branch.Bugfix: Unit tests now exist to demonstrate how it should behave, at least on Django 2.2~0.1.4Feature: the patch methods themselves (new_resolve_lookupandnew_url_render) are skipped during rendering the debug error page. The exception message and functionality remain the same.Feature: exception messages now attempt to clarify what template name (loader relative) was rendering when the missing variable was encountered.Chore: Updated themypytype signatures for some of the variables and methods to indicate the only values I’m expecting to accept.0.1.3Feature: Silenced errors when usingdebugorsql_queriesin a template, as they’re conditionally set bydjango.template.context_processors.debug0.1.2Feature: Added 11 new tokens to the default blacklist, covering more of the default Django admin and the default exception reporter.Bugfix: Changed the syntax of theAppConfig.readymethod to be py2 type annotation compatible.0.1.1Initial release