Login

Most bookmarked snippets

Snippet List

UTC-based astimezone filter

A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates. This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.

  • timezone
  • utc
  • timezones
  • times
Read More

Remove old fields on dumpdata generated json

A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.

  • json
  • loaddata
  • fixture
  • dumpdata
  • migrate
  • removed field
Read More

Persistent Session Debugging with Django Debug Toolbar

When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`. Make sure your session middleware comes before your debug toolbar middleware.

  • session
  • debug
  • debug-toolbar
Read More

Haystack objects in one query

Actually the best way to handle this is to use built-in http://docs.haystacksearch.org/dev/searchqueryset_api.html#load-all I just missed it while checking documentation and wrote this crappy snippet!

  • django
  • haystack
  • SearchQuerySet
Read More

alientag

Renders enclosing contents as it is. Useful to avoid tags conflict with certain javascript libraries (eg. jquery-tmpl.js, mustache.js) Example usage: `<script class="content-tmpl" type="text/x-jquery-tmpl">` `{% alien %}` `{{ tmpl(results) "#results .item-tmpl" }}` `{% endalien %}` `</script>`

  • templatetag
Read More

Admin related widget wrapper with edit / delete link (python model admin)

Here's the python code to produce an admin related widget with the edit and delete link also. * [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562) * [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563) * [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564) * [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)

  • admin
  • related
  • widget
  • wrapper
Read More

Admin related widget wrapper with edit / delete link (python widget)

Here's the python code to produce an admin related widget with the edit and delete link also. * [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562) * [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563) * [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564) * [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)

  • admin
  • related
  • widget
  • wrapper
Read More

django-pagination with "clean url"

To enable "clean url" mode, install [django-pagination](http://code.google.com/p/django-pagination/), then just add to settings.py PAGINATION_CLEAN_URL = True and replace '.../pagination/templatetags/pagination_tags.py' with follow, and so add to 'pagination/templates/pagination/' folder pagination_clean_url.html file, with following code **Note:** in urls.py should be like this: url(r'^/foo/bar/\d*/?$', foobar_list_handle),

  • pagination
  • clean url
Read More

Chainable custom query manager for randomize.

This manager is based on the economical method of randomization. It usable in related models. For support this behavior required to be defined as default manger.

  • queryset
  • related-models
  • randomize
  • custom-manager
Read More

SelectRelatedManager

Because select_related() only works on ForeignKeys that are not null or blank, when you are customizing the admin, even if you set "list_select_related=True" you can still end up with way too many querys in the Admin changelist. By adding this code to your model you can decrease the queries dramatically. I found I needed this when I was working with Django apps with a lot of legacy data and I couldn't set the ForeignKey null=False.

  • admin
  • manager
  • select
  • queryset
Read More

Serve admin-media from urls.py

By default the `runserver` command does some magic to automatically serve admin media. This magic doesn't happen when using other servers like gunicorn… But this makes that magic unnecessary by using urls.py to route requests for admin media to the standard static media server. `#include <production_disclaimer.h>`

  • admin
  • admin-media
Read More

Duplicating Template Tag

This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa

  • template-tag
  • duplicate
  • loop
Read More

App Display Label Template Filter Workaround

I have been wrestling with custom app labels and have come to the conclusion that this is the easiest workaround for the problem. Since I name most of my apps as Foo_Bar this makes them look a lot nicer. I just apply the filter to whatever app label and it fixes it. If you need a more complex replacement, you could just have an if statement that looks for the full app label and replaces it with whatever you want.

  • template
  • filter
  • app-label
  • app-display
Read More

Configurable defaults for contrib.sites default Site during syncdb

This simple snippet provides a more sensible default for the Site object created during the first pass of syncdb (that is, with a default domain of `localhost:8000`). I made this so that the admin's "view on site" button will work automagically during my development cycle (which often involves dropping and recreating a sqlite database). In addition, it provides 2 options for configuring the default Site as you'd like: settings parameters (`DEFAULT_SITE_DOMAIN` and `DEFAULT_SITE_NAME`) or `kwargs` (the latter takes precedence).

  • django
  • admin
  • sites
  • syncdb
  • contrib
  • manage
  • Site
  • defaults
Read More

3110 snippets posted so far.