August 30, 2007

My duh moment – Django and it’s lack of strip filter

Had a total ‘duh’ moment today at work. I wanted to per­form the equiv­a­lent of Python’s string.strip method on some Django tem­plate output.

I looked on the Django doc­u­men­ta­tion site but I couldn’t find a strip filter.

It took me a few min­utes of con­ster­na­tion to figure why the devel­op­ers wouldn’t include such an oft-​needed filter.

And then, the afore­men­tioned duh moment, I real­ized that I could just call {% myvar.strip %} {{ myvar.strip }} — because my output is a string and you can use Django’s dot syntax to call meth­ods, dic­tio­nary keys, etc.

Duh, I’m a moron.

So this post is writ­ten for Google in the hopes that some­one else sim­i­larly con­ster­nated will find this tidbit and be un-​consternated.

Filed under: Django,Python

Next:
Previous:

Related

  • Ian

    I did the same duh too!

    How­ever, its {{ myvar.strip }} rather than {% myvar.strip %}…

    :D

  • http://heisel.org Chris

    @Ian — Duh! #facepalm. Thanks for catch­ing that!

  • Greg

    Thank you! It should be doc­u­mented some­where though..

  • Jose

    I was look­ing for this. Thank you!

  • http://www.tarmack.eu Bart

    Thank you for uncon­ster­nat­ing me. :)

  • nate

    But how can you spec­ify what you want to strip?

    E.g. text.strip(“@”)

  • Sam Kuehn

    I was look­ing for this as well.  I am a moron too :)

  • Bradley Ayers

    I got caught too :(

  • Bradley Ayers

    I got caught too :(