question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I was surprised to see that a lot of values seem to be just piped through the |safe filter in the templates. This effectively disables Django’s auto-escaping functionality and requires explicit escaping of values within the form definition in Python.

Compare rendering a standard Python form:

>>> from django import forms
>>> class MyForm(forms.Form):
...   foobar = forms.CharField(label='Foo & bar')
...
>>> unicode(MyForm())
u'<tr><th><label for="id_foobar">Foo &amp; bar:</label></th><td><input id="id_foobar" name="foobar" type="text" /></td></tr>'

(notice that the label has been auto-escaped to use &amp;)

Compare this to the output from Crispy Forms:

>>> from django.template import Template, Context
>>> template = Template('{% load crispy_forms_tags %}{% crispy form %}')
>>> template.render(Context({'form': MyForm()}))
u'\n\n<form  method="post" ><div id="div_id_foobar" class="form-group"><label for="id_foobar" class="control-label  requiredField">\n\t\t\t\tFoo & bar<span class="asteriskField">*</span></label><div class="controls "><input class="textinput textInput form-control" id="id_foobar" name="foobar" type="text" /> </div></div></form>\n'

(notice that the label is unsafe and outputs just &)

To get safe and valid HTML, I need to either manually use the HTML entity:

>>> class MyForm(forms.Form):
...   foobar = forms.CharField(label='Foo &amp; bar')

Or programmatically escape it:

>>> from django.utils.html import escape
>>> class MyForm(forms.Form):
...   foobar = forms.CharField(label=escape('Foo & bar'))

Note that marking the string as unsafe does not work:

>>> from django.utils.safestring import mark_for_escaping
>>> class MyForm(forms.Form):
...   foobar = forms.CharField(label=mark_for_escaping('Foo & bar'))
...
>>> template.render(Context({'form': MyForm()}))
u'\n\n<form  method="post" ><div id="div_id_foobar" class="form-group"><label for="id_foobar" class="control-label  requiredField">\n\t\t\t\tFoo & bar<span class="asteriskField">*</span></label><div class="controls "><input class="textinput textInput form-control" id="id_foobar" name="foobar" type="text" /> </div></div></form>\n'

My understanding is that auto-escaping is best practice and that generally using mark_safe on strings in Python that you explicitly want to avoid escaping is preferred to manually escaping the values.

Is there a particular reason Crispy Forms assumes all strings are safe? Removing this behaviour would probably be a good idea, but would be backward incompatible.

Happy to put together a pull request if necessary.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
smithdc1commented, Jun 23, 2020

We’ve recently discussed bumping the major version for other breaking changes. So it sounds like this is the path the project is headed.

Will be good to see what this looks like but I’m nervous about time it may take to get everything in place to deliver the changes.

I’ll try and give some thought to a plan so we have some steps we can track.

0reactions
smithdc1commented, Jun 27, 2022

All of the |safe filters have now been removed from the templates except for help_text. This is to maintain compatibility with Django itself. See https://docs.djangoproject.com/en/4.0/ref/forms/fields/#help-text

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does it mean to escape a string? - Stack Overflow
Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a ...
Read more >
Escaping Strings in Transformations - Trifacta Documentation
This section describes how to escape strings in your transformations. ... backslash character ( \ ) is used to escape values within strings....
Read more >
Escaping strings - ETL Software
Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string,...
Read more >
Escape character - Wikipedia
An escape character is a particular case of metacharacters. Generally, the judgement of whether something is an escape character or not depends on...
Read more >
5.2. Escaping
quoted string-expansion construct is a mechanism that uses escaped octal or hex values to assign ASCII characters to variables, e.g., quote=$'\042'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found