Login

Most bookmarked snippets

Snippet List

Spaceless all HTML pages

This middleware remove all space between tags and line break of all HTML pages. Use a standard Django method. Set *force_spaceless* for dev. purpose.

  • middleware
  • optimization
  • spaceless
Read More

Sophisticated order_by sorting

I wanted to sort a CharField which consists of digits in a different way. This field is a matricle number field (some kind of registration number for students. They have matricle numbers in the format YYxxxxxx - which means "YY" are the last two digits of the year they started studying.) So I wanted to sort them in a way that they appear like this: 5000000, 5000001, ... , 9999998, 9999999, 0000000, 0000001, ... , 1200000, ... , 4999999 Took me some time to find out how to do this efficiently in PostgreSQL, and so I thought I'd share it here. The important stuff is in the model "Candidate" to use a special "objects" object manager which uses a special QuerySet as well. Here lies the "magic": If there is a ordering required that contains "mnr", then a special on-the-fly calculated field will be added to the queryset called "mnr_specialsorted". Now it is possible to do things like `Candidate.objects.filter( firstname__contains="Pony" ).exclude( lastname__contains="Java" ).order_by("lastname", "-mnr")` For other database engines you might want to change the MNR_SORTER variable to fit your needs.

  • sort
  • orderby
  • sorting
  • ordering
Read More

Trim the center of a string

I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly. So i wrote this little function to cut the center of a string i thought it cute so i leave it here. Pay attention to the comment so you can see what is going on.

  • python
  • string
  • slice
Read More

Use django-social-auth & Google Accounts for admin login

1. Create an app and place this in `admin.py`. 2. Add `url(r'^login/$', 'social_auth.views.auth', {'backend': 'google'}, name='login')` to your `urls.py`. 3. Add the app to your `INSTALLED_APPS` after `django.contrib.admin`. 4. Set `USE_SOCIAL_AUTH_AS_ADMIN_LOGIN = True` in your `settings.py`. 5. ... 6. Profit.

  • admin
  • login
  • auth
  • google
  • openid
  • django-social-auth
Read More

Expand(flatten) url patterns

This is a small function for those time when you want a list of all your urls, expanding included urls, so in the end is like all your urls are in one module. This function recursively does it, so it doesnt matter how nested the includes are in the end you get one flat list.

  • urlpatterns
Read More

Tatsypie: additional list endpoints for custom Model's manager methods

Although configuring filtering in TastyPie is possible, it is limited to per-field filters, which are not enough for more complex filtering. If your model implement custom manager methods for complex filters, exposing these methods as TastyPie Resource list endpoints is not an easy task. The ModelResource subclass provided here does this, providing 3 ways of complex filtering: * define querysets for filters directly in the Resource declaration * use Model manager custom method * use QuerySet custom method

  • managers
  • manager
  • custom-manager
  • tastypie
Read More

Run a testcase with custom INSTALLED_APPS

This code allows you to register a model to Django that is only used for unit testing. It will not exist in the regular Django workflow. After the tests executed, the Django settings are restored. Usage: 1. Change `tests.py` into a `tests` package. 2. Place a `models.py` in the `tests` package. 3. Use the following code below to enable it. Example: class MyTest(CustomSettingsTestCase): new_settings = dict( INSTALLED_APPS=( 'django.contrib.contenttypes', 'django.contrib.auth', 'app_to_test', 'app_to_test.tests', ) ) Based on http://djangosnippets.org/snippets/1011/ as Django 1.4 version

  • settings
  • testing
  • test
  • syncdb
Read More

nbsp filter

Replaces usual spaces in string by non breaking spaces. "some words" --> "some words" Usage in template: {% load nbsp %} .... {{ user.full_name|nbsp }}

  • templatetag
  • nbsp
Read More

User activation codes without additional database tables or fields

UserAuthCode generates an authentication code for Django user objects. This code can be used to verify the user's email address and to activate his account. Unlike other solutions there's no need add any tables or fields to your database. Current version is hosted on [GitHub](https://github.com/badzong/django-userauthcode). There's also an example how to use it in your Django project.

  • user
  • account
  • activation
Read More

boostrap append-input for widgets

Append span with text, image or other data to any django widget so bootstrap can format it like in [here](http://twitter.github.com/bootstrap/base-css.html#forms) (scroll to "Extending form controls" section) Example usage: ` example_field = CharField( max_length=255, min_length=1, label='Label', required=False, widget=AppendWidget(base_widget=TextInput, data='@') ) `

  • django
  • bootstrap
  • append-input
Read More

3110 snippets posted so far.