Skip to content

Fix render_field losing boolean type for attributes like checked - #170

Open
juneja-varun wants to merge 1 commit into
django-commons:masterfrom
juneja-varun:fix-render-field-boolean-attrs
Open

juneja-varun wants to merge 1 commit into
django-commons:masterfrom
juneja-varun:fix-render-field-boolean-attrs

Conversation

@juneja-varun

Copy link
Copy Markdown

Fixes #104.

I had a checkbox rendered with {% render_field form.is_completed checked=some_bool %}, and when some_bool was False it still rendered checked="False" - which every browser treats as checked, since HTML has no falsy value for a boolean attribute, only presence/absence.

Root cause: render_field resolves the attribute value from the template, then hands it to set_attr/append_attr by building a string like f"checked:{value}" - which stringifies the value before it gets split back apart. So a real Python False becomes the literal text "False", and Django's own widget rendering (which checks value is not False/value is not True to decide whether to omit the attribute or render it bare) never sees the actual boolean.

Fixed by having render_field hand the resolved value straight through, without the round trip through a string. Kept the attribute name's existing :: escaping (documented in the README for names with a literal colon, like v-bind::class) working the same as before - caught a regression in my first draft here via the existing test_alpinejs_event_modifier test, which exercises exactly that. Added a test for the checked=True/False case on both directions.

@juneja-varun
juneja-varun force-pushed the fix-render-field-boolean-attrs branch from c4c64bf to 1ae671c Compare September 7, 2026 20:03
@juneja-varun

Copy link
Copy Markdown
Author

Just checking in on this one — happy to address any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

render_field should not even add the "checked" attribute to inputs when checked=False

1 participant