Login

All snippets

Snippet List

Precise truncate chars filter

Template filter that truncates the text when it exceeds a certain number of characters. It deletes the last word only if partial. Adds '...' at the end of the text, only if truncated. Examples (text == 'Lorem ipsum dolor sit amet', len(text) == 26) {{ text|truncatewords_by_chars:30 }} 'Lorem ipsum dolor sit amet' {{ text|truncatewords_by_chars:25 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:21 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:20 }} 'Lorem ipsum dolor...' By Davide Muzzarelli

  • filter
  • template-filter
  • truncate
  • truncatewords
Read More

Template filter that divides a list into exact columns

Template filter that divides a list into an exact number of columns. The number of columns is guaranteed. Example (list == [1,2,3,4,5,6,7,8,9,10]): {% for column in list|columns:3 %} <ul> {% for item in column %} <li>{{ item }}</li> {% endfor %} </ul> {% endfor %} Result: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ul> <li>5</li> <li>6</li> <li>7</li> </ul> <ul> <li>8</li> <li>9</li> <li>10</li> </ul> By Davide Muzzarelli

  • filter
  • template-filter
  • list
  • columns
Read More

haystack auto update (forget about update_index)

Dead simple snippet. Paste it in some models (i use project_specific/models.py), and you don't need to run update_index anymore. When a model is deleted from the database: it is deleted from the index. When a model is saved (created or modified): it is updated in the index.

  • haystack
Read More

models.MultipleEmailField

Define validator `multiple_email_validator` that splits value by commas and calls `validate_email` validator for each element found. Then define MultipleEmailField with this default validator and augmented max_length. Then ... use it!

  • models
  • fields
  • validators
Read More

Generic compact list_filter with counts

Based on [#1879](http://djangosnippets.org/snippets/1879/) and [#2356](http://djangosnippets.org/snippets/2356/) Works in Django 1.3 Hopefully it's generic enough to implement a compact (sparse) version of whatever custom filter you need.

  • admin-interface
  • compact
  • list_filter
Read More

JSON instead of pickle for memcached

Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) doesn't know pickle, but json yes.

  • memcache
  • cache
  • json
  • memcached
  • pickle
Read More

Save image in field

Small function that i use to save files to an imagefield, the file can be either an url or a file. This function has the following requirements. import requests from django.core.files import File from django.core.files.temp import NamedTemporaryFile from django.conf import settings Get the awesome requests library: pip install requests This function uses MEDIA_ROOT to know the location of the static dir, you can change that chaging the static_dir variable.

  • image field
  • save file
Read More

Multilingual site based on domain - not accept header and django_session

On our site [Fornebuklinikken - A cosmetic surgeon in Norway](http://www.fornebuklinikken.no) we also have a domain [http://fornebuklinikken.com](http://www.fornebuklinikken.no) which should be using the 'en' language. We didn't wan't to use the standard locale lib, and wrote our own middleware which lookups the correct language corresponding to the domain (.no or .com) Any questions? Contact me on herman.schistad (at) gmail.com

  • internationalization
  • middleware
  • multilingual
  • locale
  • domain
  • localeurl
Read More

SOAP web service with soaplib 2.0

I've updated the `DjangoSoapApp` class from [this popular soaplib snippet](http://djangosnippets.org/snippets/2210/) so the snippet will work properly with soaplib 2.0. Usage is the same as before: my_soap_service = DjangoSoapApp([MySOAPService], __name__)

  • soap
  • soaplib
  • wsdl
Read More

manage.py reboot

1) Simply create a "management/commands" folder in one of your INSTALLED_APPS folders. Then add a "reboot.py" file in your "management/commands" 2) In settings.py, you can OPTIONALLY add: PROJECT_NAME = 'Blah' PROJECT_DOMAIN = 'blah.com' These two settings will be used to create a "Site" object as well as a superuser. If you choose not to use these settings, the reboot command simply reverts to using your DATABASES[default] username as superuser. 3) Execute the command via "python manage.py reboot" Enjoy.

  • runserver
  • setup superuser
  • recreate database
  • install fixtures
Read More

Validation for full e-mails (e.g. "Joe Hacker <[email protected]>")

Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, `[email protected]` is accepted. On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django: Joe Hacker <[email protected]> This package adds support for validating full e-mail addresses. **Database model example** from django import models from full_email.models import FullEmailField class MyModel(models.Model): email = FullEmailField() **Forms example** from django import forms from full_email.formfields import FullEmailField class MyForm(forms.Form): email = FullEmailField(label='E-mail address') I maintain this code in a [GitHub gist](https://gist.github.com/1505228). It includes some unit tests as well.

  • forms
  • model
  • email
  • validation
  • orm
  • database
Read More

Template filter to markup form fields with optional args

Template filter to mark-up individual form fields. Usage : In template - {% load form_custom %} then for a form field - {{ form.field|form_row:"default" }} for default wrapper or - {{ form.field|form_row:"lbl_cls=some_class_name&reqd=no" }} to pass option args seperated by & Optional args are :- wrapper_cls - override default field wrapper div class name error_cls - override default field error div class name lbl_cls - override default label_tag div class name label - yes/no default is yes - output label_tag reqd - yes/no default is yes - marks up required fields as bold with label ending with * See code for all default args.

  • filter
  • forms
Read More

3110 snippets posted so far.