Login

Most bookmarked snippets

Snippet List

Wrappable text

Sometimes you have an uncontrolled amount of text in a horizontally constrained space. The wrappable filter places zero-width breaking spaces into the given text so that it can wrap at any point, as necessary for the containing width. Sometimes better than eliding (chopping long text...) or cropping/scrolling overflow.

  • template
  • filter
  • typography
Read More

Integer based MoneyField

It is supposed the aggregation on integer is fast than numeric type in database duo to how they are stored as numeric is represented as string. As money only have 2 decimal place, it can be converted to an Integer despite of its decimal point. The python class decimal.Decimal also has a shortcoming that it is not json serializable(neither cjson nor simplejson). This money field appears as a float number to the front end, so it does not meet the headache as DecimalField. The usage is very simple. In Model: class SomeModel(models.Model): ... income = MoneyField('income') ... Then treat the attribute of the model instance as a normal float type. **Notes** If you try to use aggregation on this field, you have to convert it to float by yourself. Currently I could not find any method to fix this.

  • money
  • db
  • field
Read More
Author: Jay
  • 0
  • 1

simple tag-cloud Template Tag

This Template Tag computes the font-size of two given arguments and returns a CSS-encoded string like "font-size: XXpx;", which can be used to format the font-size of link. (The minium font-size must be set with CSS.) Requires two arguments: 1. occurrence of the current tag 2. sum of all occurrences It works great for my tag-cloud. [Source of the logarithmic formula](http://www.php.de/php-fortgeschrittene/44928-tag-cloud-algorithmus-fuer-schriftgroessye.html) **Usage** `<a href="http://www.anything.com" {% cloudify variable.occurrence overall. occurrence_sum %} title="anything">any tag</a>`

  • tag
  • cloud
  • logarithmic
Read More

truncatechars filter

Truncates a string after a certain number of chars. Question: > *Why don't you use the built-in filter slice?* I need the "three points" (...) only when it really truncates.

  • template
  • filter
  • truncatewords
Read More

Save a model using an arbitrary db connection

This function lets you save an instance of a model to another database based on a connection argument. Useful when doing data migrations across databases. Connection is anything that would work as a django.db.connection I'm not sure if this handles proxy models or model inheritance properly, though.

  • multi-db
  • connection
Read More

Fix duplicate first page of paginated results

Search engines might conclude there's duplicate content if `/some_view/` and `/some_view/?page=1` returns the same results. This middleware redirects `?page=1` to the URL without the page parameter. You can set the name of the parameter in settings.py as `PAGE_VAR`. See [here](http://www.muhuk.com/2009/08/a-civilized-way-display-lots-of-data/) for more details.

  • middleware
  • pagination
  • seo
  • paginate
Read More

Template Filter: Add indentation

Template filter to add the given number of tabs to the beginning of each line. Useful for keeping markup pretty, plays well with Markdown. Usage: {{ content|indent:"2" }} {{ content|markdown|indent:"2" }}

  • filter
  • markup
  • pretty
  • indent
  • indentation
Read More

Auto-rename duplicate fields

This is useful to run before you add a unique key to a character field that has duplicates in it. It just adds numbers to the end of the contents, so they will be unique. It takes a model class and a field name. The model class can be a South fake orm object, so this can be used inside data migrations.

  • rename
  • duplicate
  • south
  • unique-key
Read More

Printing inline formsets as UL / P

By default all forms created using inlineformset_factory are displayed as tables (because there is only a .as_table method) and there are no .as_p or .as_ul methods in them, so you need to do that by hand.

  • django
  • formset
  • inline
  • not-django-admin
Read More

CharField powered Tags with ChoiceField widget.

Shell example: >>> pprint.pprint(settings.FORUM_TAGS) ((u'x11', 'Xorg'), (u'pacman', 'Pacman'), (u'aur', 'AUR'), (u'abs', 'ABS'), (u'howto', 'HOWTO'), (u'instalacja', 'Instalacja'), (u'offtopic', 'Offtopic')) >>> t = Thread.objects.all()[0] >>> pprint.pprint(t.tags) [{'slug': u'pacman', 'title': 'Pacman'}, {'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'instalacja', 'title': 'Instalacja'}, {'slug': u'offtopic', 'title': 'Offtopic'}] >>> t.tags = [{'slug': 'abs'}, {'slug': 'howto'} >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}] >>> t.tags = ['Offtopic', 'HOWTO'] >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'offtopic', 'title': 'Offtopic'}]

  • tag
  • forms
  • field
  • selectmultiple
  • checbox
Read More

Limit ManyToMany fields in forms

Limit ManyToMany fields in forms. Hide the field, if only one item can be selected. e.g. For limit sites choices only to accessible sites. Also available via django-tools: http://code.google.com/p/django-tools/

  • forms
  • manytomany
Read More

match filter

A filter that re.matches a regex against a value. Useful for nav bars as follows: {% if location.path|match:"/$" %} class="current"{% endif %} For `location.path` see my [location context_processor](/snippets/1685/).

  • regex
  • match
  • nav
Read More

3110 snippets posted so far.