Suppose I’ve got a custom form label with some HTML on it like so:
SafeUnicode('<span class="superscript">™</span>')
Why would Django 1.2 have a function mark_safe if this exist? What are the differences if any?
Thanks for the help!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
mark_safeis a factory function which encapsulate a bit of type-checking logic in order to return, as appropriate, either aSafeUnicodeor aSafeString(or possibly some other subclass ofSafeDatashould you have defined any such subclasses). The source is easily short enough to quote…:Just using
SafeUnicode(s)instead ofmake_safe(s)will be minutely faster, but could get you in trouble if you’re potentially dealing with a type and value that don’t happily support being passed to theSafeUnicodeinitializer (e.g., a byte string with non-ascii codes, a non-string, aPromisewith a string delegate, …). If you are 100% certain that you know what you’re doing, nothing stops you from going for the nanoseconds-saving approach;-).By the way, some questions about open-source code (no matter how well documented, and Django’s docs are really impressive) are often best answered by first having a look at the code (and then asking if the code’s too complex or subtle to follow with assurance).