Opened 10 days ago

Last modified 7 days ago

#28874 assigned Bug

Errors on hidden input fields print out double escaped HTML

Reported by: Kyle Agronick Owned by: Daniil Ryzhkov
Component: Template system Version: 1.11
Severity: Normal Keywords:
Cc: Daniil Ryzhkov Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: yes

Description

Hidden input fields will print errors like "It\'s the thing &" as "(Hidden field test) It's the thing &". with HTML that is double escaped as

<ul class="errorlist nonfield">
    <li>(Hidden field test) It&amp;#39;s the thing &amp;amp;</li>
</ul>

Using this code:

views.py

class TestForm(forms.Form):
    test = forms.CharField(widget=forms.HiddenInput(), required=False)
    foo = forms.CharField()

class StoreGroupDelete(FormView):
    form_class = TestForm
    template_name = 'test/test_delete.html'

    def form_valid(self, form):
        form.add_error('test', 'It\'s the thing &')
        return self.form_invalid(form)

test.html

        <form method="post">
            {% csrf_token %}
            <ul>
            {{ form.as_ul }}
            </ul><input type="submit" />
        </form>

This only happens on hidden inputs. Regular fields work fine. This is on 1.11.7.

Change History (6)

comment:1 Changed 9 days ago by Daniil Ryzhkov

I was able to reproduce this behaviour and can confirm that this only happens for HiddenField. It works normally for CharField.

comment:2 Changed 9 days ago by Daniil Ryzhkov

I've checked Django code and it seems to be a django bug.
This line in django.forms.forms breaks affect of mark_safe:

According to git blame, this issue should should be reproducible in every django release for last 5 years:
I was able to fix this. I will write tests to cover this issue and submit my changes as PR on github.

comment:3 Changed 9 days ago by Daniil Ryzhkov

Cc: Daniil Ryzhkov added
Needs tests: set
Owner: changed from nobody to Daniil Ryzhkov
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:4 Changed 8 days ago by Daniil Ryzhkov

Triage Stage: AcceptedReady for checkin
Last edited 8 days ago by Daniil Ryzhkov (previous) (diff)

comment:5 Changed 7 days ago by Simon Charette

Triage Stage: Ready for checkinAccepted

Please don't mark your own patch as RFC.

comment:6 Changed 7 days ago by Daniil Ryzhkov

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