The Python Oracle

Django: How to apply conditional attribute to HTML element in template?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Jungle Looping

--

Chapters
00:00 Django: How To Apply Conditional Attribute To Html Element In Template?
00:45 Accepted Answer Score 15
00:56 Answer 2 Score 24
01:03 Thank you

--

Full question
https://stackoverflow.com/questions/2864...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #python27 #djangotemplates

#avk47



ANSWER 1

Score 24


Why not wrap the attribute in a conditional?

<input type="checkbox" name="sendEmail" {% if customer.SendSms %}checked{% endif %}>



ACCEPTED ANSWER

Score 15


yesno template filter will do the job:

<input type="checkbox" name="sendSms" {{ customer.SendSms|yesno:"checked" }}>