Login

Most bookmarked snippets

Snippet List

When saving in admin it will call model save() twice.

When I save this in admin, it will call model save() twice Django version 1.2.3, using settings 'admin.development' Development server is running at http://127.0.0.1:8080/ Quit the server with CONTROL-C. [18/May/2011 10:08:22] "GET /admin/onixcodes/bookbatch/ HTTP/1.1" 200 38211 [18/May/2011 10:08:22] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 [18/May/2011 10:08:29] "GET /admin/onixcodes/bookbatch/55/ HTTP/1.1" 200 9811 [18/May/2011 10:08:29] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 save!! save!! [18/May/2011 10:08:33] "POST /admin/onixcodes/bookbatch/55/ HTTP/1.1" 302 0 [18/May/2011 10:08:33] "GET /admin/onixcodes/bookbatch/ HTTP/1.1" 200 38364 [18/May/2011 10:08:33] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 Anyone has any suggestion? Thanks.

  • admin
  • save()
  • twice
Read More

Pygmentify a code using template filter

The above snippet can be added as a custom filter for rendering code snippets in django templates. A simple '|render' next to any source code will do. To add css, use the following command to generate css file. pygmentize -S emacs -f html > pygments-colorful.css And link this css file to the template.

  • template
  • django
  • pygments
  • custom-filters
Read More

Other translation block

Use in a with statement to set the translation to another locale for a block >>> from django.utils.translation import ugettext >>> ugettext('title') u'title' >>> with Translation('fr') as locale: ...: print locale.locale ...: print ugettext('title') ...: ...: fr titre >>> ugettext('title') u'title'

  • i18n
  • mail
  • translation
Read More

CollectionFrom (GeometryCollection <-> Geometry Fields!)

Motivation: We can't use GeometryCollections to do filters, etc in GeoDjango due to incomplete underlying libraries. But with this CollectionFrom field we can get all the benefits of working with GeometryCollections but still query based on points, lines, polys. If you're using GeometryCollectionFields and see this error: DatabaseError: Relate Operation called with a LWGEOMCOLLECTION type. This is unsupported. Then this will probably be helpful for you.

  • geodjango
  • geo
  • geometrycollection
Read More

Simple template tag to do |stringformat filter with format from a variable

Formats a django variable using a python string formatting as specified in another template variable. Similar to |stringformat. Takes two arguments: the django template variable with the item to be formatted and the django template variable containing the format string. {% pyformat number formatstringvar %} Place this file in `appname/templatetags/pyformat.py` and you're good.

  • template-tag
  • string-formatting
Read More

South introspection rules for TimeZoneFields

TimeZoneField is not supported with South 0.7. This snippet adds a custom rule to the introspection rules for TimeZoneField. The code is found in the following thread, and fixed according to the latest version of TimeZoneField. http://groups.google.com/group/south-users/browse_thread/thread/34a05331140ee4dd **Usage** Simplest way is to save this snippet in a south_rules.py file and add an import in the models.py.

  • timezones
  • south
  • timezonefield
Read More

Deep package test runner

**NOTE: This is modified from 1.0's test runner and has only been tested on Django 1.0 + Python 2.5** This is a test runner that searches inside any app-level "tests" packages for django unit tests. Modules to be searched must have names that start with "test_" (this can be changed by modifying `get_multi_tests()`, [`mod.startswith('test_') and mod.endswith('.py')`]). It also allows for arbitrarily nested test packages, with no restrictions on naming, so you could have: myapp/ +- tests/ +- __init__.py +- test_set1.py +- category1/ +- __init__.py +- test_something.py +- subcat/ +- __init__.py +- test_foobar.py +- category2/ +- __init__.py +- test_other.py and "manage.py test myapp" would pick up tests in all four test_*.py test modules. Searching the modules in this way, instead of importing them all into the top-level `__init__.py`, allows you to have "name collisions" with TestCase names -- two modules can each have a TestFooBar class, and they will both be run. Unfortunately, this snippet doesn't allow you to specify a full path to a specific test case or test module ("manage.py test myapp.category1.test_something" and "manage.py test myapp.test_set1.TestFooBar" won't work); using "manage.py test myapp.TestFooBar" will search out all test cases named "TestFooBar" and run them. "manage.py test myapp.TestFooBar.test_baz" will work similarly, returning the test_baz method of each TestFooBar class. To use, put this code in "`testrunner.py`" in your project, and add `TEST_RUNNER = 'myproject.testrunner.run_tests'` to your `settings.py`.

  • tests
  • test-runner
  • package
Read More

Truncate text to length up until the nearest space

This will truncate a long character based on the given length parameter. If the word is cut-off, it will return the string up until the next space. If there are no spaces in the next 5 characters, that should mean a very long word and we should truncate right away.

  • template
  • string
  • truncate
Read More

KMLMiddleware

This is a slight modification from the original version at [http://djangosnippets.org/snippets/709/](http://djangosnippets.org/snippets/709/) and is a middleware designed to output the right headers for generated KML or KMZ content. In the case of KMZ content, it handles the compression of the original KML content into the KMZ format. This version applies the processing to only requests ending with a .kml or .kmz in the URL. For instance, a request with the URL **http://example.com/kml/foo.kml** or **http://example.com/foo.kmz** will get processed by this middleware.

  • google-maps
  • kml
  • kmz
  • google-earth
Read More

Template Tag for Retrieving complex Settings

Very much like the snippet joshua wrote (http://djangosnippets.org/snippets/67/) for the same purpose but with the addition that this lets the user get values for more complex settings like dicts and lists

  • settings
  • template tag
Read More

Improved Pickled Object Field (Fixed for Django 1.2)

Small changes to [Snippet 1694](http://djangosnippets.org/snippets/1694/) to that QueryAPI works for django 1.2 and higher. Changes: * Replaced `get_db_prep_value` with `get_prep_value`. * Replaced `get_db_prep_lookup` with modified `get_prep_lookup`.

  • model
  • db
  • orm
  • database
  • pickle
  • object
  • field
  • type
  • pickled
  • store
Read More

3110 snippets posted so far.