Login

Top-rated snippets

Snippet List

Syntax highlighting for tracebacks in console output

This is hardcoded to use [django-discover-runner](http://pypi.python.org/pypi/django-discover-runner) since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the `Terminal256Formatter` formatter instead. Enabled it with the `TEST_RUNNER` setting: TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner' Where `dotted.path.to.highlighted.runner` is the Python import path of the file you saved the runner in.

  • pygments
  • testing
  • test
  • traceback
Read More

Database backup with admin command

Detect type of database (MySQL, PostgreSQL or SQLite) and make backup. In this moment ONLY WORK in GNU/Linux, NOT WIN.

  • database
  • admin-actions
  • backup
  • MySQL
  • admin-command
  • SQLite
  • PostgreSQL
Read More
Author: jhg
  • 1
  • 3

Closure for FieldListFilter classes with custom sets of ranges

This closure lets you quickly produce date-style range filters in the Django Admin interface without having to create a new class for each one. It follows Python range semantics, with the lower value using a `_gte` test and the upper value using an `_lt` test. Here's an example of how I'm using it in one of my projects: list_filter = ('complete', ('chapters', makeRangeFieldListFilter([ (_('1'), 1, 2), (_('2 to 10'), 2, 10), (_('11 to 30'), 11, 30), (_('31 to 100'), 31, 100), (_('At least 100'), 100, None), ], nullable=True)), ('word_count', makeRangeFieldListFilter([ (_('Less than 1000'), None, 1000), (_('1K to 5K'), 1000, 5000), (_('5K to 10K'), 5000, 10000), (_('10K to 75K'), 10000, 75000), (_('75K to 150K'), 75000, 150000), (_('150K to 300K'), 150000, 300000), (_('At least 300K'), 300000, None), ], nullable=True)), ('derivatives_count', makeRangeFieldListFilter([ (_('None'), 0, 1), (_('1 to 5'), 1, 5), (_('5 to 50'), 5, 50), (_('50 to 1000'), 50, 1000), (_('At least 1000'), 1000, None), ])), 'pub_date', 'upd_date') It is based on code from `DateFieldListFilter` and `BooleanFieldListFilter` from `django.contrib.admin.filters`.

  • filter
  • django
  • admin
  • fieldlistfilter
Read More

Digg-style pagination, and retain GET params

This snipplet is based on [Ryan Kanno's work](http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/) and [snipplet #2680](http://djangosnippets.org/snippets/2680/). The page numbers are basically composed by `pages_outside_trailing_range`, `page_range` and `pages_outside_leading_range`. The GET params in the request(except for `page` itself) are retained across different pages. This is important when you have an `order_by` param working inline.

  • pagination
Read More

yandex maps templatetag

https://github.com/coolchevy/django-yandexmaps django-yandexmaps ================= easy maps integration via yandex maps api Installation ============ 1. Add your YANDEXMAPS_API_KEY to settings.py http://api.yandex.ru/maps/form.xml/ 2. Add 'yandex_maps' in INSTALLED_APPS Usage ============ Template:: {% load yandexmaps_tags %} {% yandex_map_by_address object.get_address object.title 500,300 %} Demo ============ http://colorsound.com.ua/clubs/porter-pub-1/

  • template
  • django
  • templatetag
  • api
  • templatetags
  • yandex
  • maps
Read More

True Unique Boolean Model Decorator

This class decorator will help you when you want to keep a unique boolean (think a 'default' field which should only be only one set to true in a group). The interesting thing with this, is that it's possible to assign a subset of fields to be used, so that the uniqueness of the field will be guaranteed among only the subset (look at the Example section in the code to understand this behaviour). This is a better and improved way of doing http://djangosnippets.org/snippets/2676/

  • model
  • unique
  • boolean
Read More

Load response.content in browser (for debugging)

When debugging tests you frequently need to inspect response content, making a pdb. set_trace() breakpoint and printing response.content but html isn't enough human readable (even for programmers :D) so, why not open it in your browser? Suposse you save this code in utils.py and you break your testcase as this: response = self.client.get(self.url) import pdb; pdb.set_trace() Then: (pdb) from utils import load_response_on_firefox (pdb) load_response_on_firefox(response) Ta-Da!

  • debug
  • testing
  • response
  • response.content
Read More

3110 snippets posted so far.