You know those bugs that take* hours* to debug? And the fix is ... moving a single line.
That kind of bug is tripped up when you use django's csrf protection and someone comes to your site using IE9. It can easily happen that not only will form stylings not affect the elements, the form might even ignore any values entered!
But it works perfectly on older versions of IE.
The bug was insanely difficult to pinpoint because I don't have IE9 anywhere - only linux and mac around here, any windows box is so old IE7 is about the extent of its capabilities ... After installing some sort of windows 7 to a virtual machine and getting IE9 working, I set to work.
Symptoms
- Form works perfectly in IE7, Chrome, Firefox etc.
- Form works perfectly with compatibility mode turned on
- Form values aren't submitted
- Form elements don't respond to form styling
For some reason IE9 thinks the form is empty. Displays all elements, but they don't behave like they're part of the form ... which is kind of odd. They aren't even selectable through the javascript console or anything.
Solution
After many pointless wanderings and random code changes, I realized the HTMLdoesn't look like something I'd usually write (legacy-ish code).
<form action="/somewhere" method="POST">
{% csrf_token %}
<label>blah</label><input type="text" name="blah" />
<!-- etc -->
</form>
Usually I'd put the {% csrf_token %}
the end. Not sure why, just the way I've always done it ... surely this can't make a difference right?
It can!
Putting it at the bottom of the form fixed all bugs. Styling suddenly worked, form was submitted with all data and everything worked everywhere.
Le huh?
When the form is rendered that {% csrf_token %}
is replaced with an invisible div tag. For some reason this ticks off IE9's strictness and the rest of the form isn't considered to be a part of this element anymore.
Something like this:
<form method="POST" class="filter" style="display: none;">
<div style="display: none;">
<input
type="hidden"
name="csrfmiddlewaretoken"
value="41aeb46067bf9e800f9ce6c718265121"
/>
</div>
<input type="hidden" name="selected" value="" />
<label for="dateFrom">From date:</label>
<input type="text" class="datepicker" name="dateFrom" readonly value="" />
<!-- etc. -->
</form>
I can't find official reference either way, but I think div's inside forms are legal HTML, perhaps not the best looking HTML, but p tags are allowed, even happen in the form tag example on w3.org, so I have no idea why IE9 would trip up on this.
Perhaps someone a bit more knowledgeable of strict HTML can shed some light on this issue? For now, let's all just be aware of this little quirk.
Continue reading about Django and IE9 don't play well together
Semantically similar articles hand-picked by GPT-4
- jQuery imageless buttons a la Google
- jQuery imageless buttons a la Google take two
- Django protip #2: Forms are awesome
- jQuery imageless buttons a la Google get an update
- A lesson about client-side templating
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️