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

Had a total ‘duh’ moment today at work. I wanted to perform the equivalent of [Python’s string.strip method](http://www.python.org/doc/2.3/lib/module-string.html#l2h-819) on some [Django template output](http://www.djangoproject.com/documentation/templates/).

I looked on the [Django documentation site](http://www.djangoproject.com/documentation/templates/) but I couldn’t find a strip filter.

It took me a few minutes of consternation to figure why the developers wouldn’t include such an oft-needed filter.

And then, the aforementioned duh moment, I realized 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 methods, dictionary keys, etc.

Duh, I’m a moron.

So this post is written for Google in the hopes that someone else similarly consternated will find this tidbit and be un-consternated.

About Chris

Python developer, Agile practitioner trying desperately not to be a pointy haired boss.
This entry was posted in Django, Python. Bookmark the permalink.

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

  1. Ian says:

    I did the same duh too!

    However, its {{ myvar.strip }} rather than {% myvar.strip %}…

    😀

  2. Greg says:

    Thank you! It should be documented somewhere though..

  3. Jose says:

    I was looking for this. Thank you!

  4. Bart says:

    Thank you for unconsternating me. 🙂

  5. nate says:

    But how can you specify what you want to strip?

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

  6. Sam Kuehn says:

    I was looking for this as well.  I am a moron too 🙂

  7. Bradley Ayers says:

    I got caught too 😦

  8. Bradley Ayers says:

    I got caught too 😦

  9. Marcin says:

    Thanks man, I got caught, too!

  10. Yevgeniy Shchemelev says:

    +1 moron

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s