Login

Most bookmarked snippets

Snippet List

Convert numbers in USA notation to brazilian notation

**This is the converter, just put it as a filter and call it with a float number.** * So, if you have a template variable **{{ my_number }}** that is "3096.44". * It will convert to "3.096,44" using the filter **{{ my_number|numBR }}**.

  • convert
  • float
  • usa
  • br
  • brazil
Read More

Add URL Segments to Templates

Add this code to you your context_processors.py in your project and then install it in your settings.py TEMPLATE_CONTEXT_PROCESSORS. In your template you can print out a segment of a url by using {{ segment_1 }}. For example if you're on the page "/mysite/section1/section2/" and you used {{ segment_2 }} it would print section1. This idea was taken from Expression Engines URL Segments, http://expressionengine.com/docs/templates/globals/url_segments.html. This comes in handy if you only want to do something in your template if the page your on has a particular segment. FYI, I haven't used this in a production setting yet so it could be buggy still.

  • template
  • url
  • path
  • segment
Read More

List all Form Errors

You can place this code above your form and it will list out all errors in your form if there are errors. Sometimes I like listing the errors at the top of the form because I think it looks cleaner. Make sure you add error_messages={'required': 'My detailed error here'} in your view.py for your form.

  • error
  • form
Read More

Localized digits (re)

Another one like [980](http://www.djangosnippets.org/snippets/980/), but using re's instead. I haven't benchmarked these, but my guess is that [981](http://www.djangosnippets.org/snippets/981/) is faster for strings of pure digits and this is faster for larger chunks of text that happen to contain digits. If you're generating the numbers yourself, I'd just use 981 on a number right when you generate it.

  • i18n
Read More

Localized digits

This is based on [980](http://www.djangosnippets.org/snippets/980/), removing the unnecessary use of StringIO. Hopefully the translation can be educational.

  • i18n
Read More

Localized digits

Arabic and Farsi languages use their own digits. This template filter translates any digits in the supplied unicode string into the correct ones for the language. The previous version used StringIO to parse the string one character at a time. It now uses regular expressions. I just saw that kcarnold created two snippets that also removed the need for StringIO: [981](http://www.djangosnippets.org/snippets/981/) and [982](http://www.djangosnippets.org/snippets/982/). That last snippet is almost the same as this one.

  • i18n
Read More

ImageFSStorage

A custom FileSystemStorage made for normalizing extensions. It lets PIL look at the file to determine the format and append an always lower-case extension based on the results.

  • storage
  • images
Read More

3110 snippets posted so far.