Login

Top-rated snippets

Snippet List

Custom collectstatic that uses etag and md5 digests to determine whether files on S3 have changed

For use with S3 BotoStorage STATICFILES_STORAGE ="storages.backends.s3boto.S3BotoStorage" and AWS_PRELOAD_METADATA = True Custom management command that compares the MD5 sum and etag from S3 and if the two are the same skips file copy. This makes running collect static MUCH faster if you are using git as a source control system which updates timestamps.

  • s3
  • amazon
  • aws
  • boto
  • collectstatic
  • storages
Read More

Drag and drop ordering of admin list elements for Grappelli [v2]

Adds drag-and-drop ordering of rows in the admin list view for Grappelli. This is a updated version of Snippet [#2306](http://djangosnippets.org/snippets/2306/) that works with the current version of Grappelli. The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

  • django
  • sorting
  • django-admin
  • sortable
  • grappelli
Read More

Django-pagination with Bootstrap CSS

A quick django-pagination template fix to make it work with Bootstrap CSS Library 2.0. Usage: Install django-pagination and save the snippet in templates/pagination/pagination.html then {% paginate %} will work fine. See [Django-paginaation on Bootstrap CSS](http://ronbeltran.blogspot.com/2012/05/django-pagination-bootstrap-css.html)

  • django-pagination
  • bootsrap-css
Read More

txt2img tag to show on the web text as images

txt2img tag shows on the web text as images, helping to avoid get indexed email address and some other information you don't want to be on search engines. Usage: `{{worker.email|txt2img:18|safe}}`

  • tag
  • text
  • image
  • templatetag
  • hide
  • convert
Read More

Filter by taggit tags in the admin

A FilterSpec that can be used to filter by taggit tags in the admin. To use, simply import this module (for example in `models.py`), and add the name of your TaggableManager field in the list_filter attribute of your ModelAdmin class.

  • filter
  • admin
  • taggit
Read More

View all log entries in the admin

By default, you can only see the action log ("History") for particular model instances and a list of your own actions on the admin's index. This adds a fully-fledged admin view for the LogEntry model, where you can filter actions by user, content type, action type, browse by change date, and also search in the change message. Add the code to any of your apps' admin.py. The entries will be visible only to superusers and won't be editable.

  • admin
  • logentry
Read More

Allow disabling options in a select widget

HTML allows an option in a <select> to be disabled. In other words it will appear in the list of choices but won't be selectable. This is done by adding a 'disabled' attribute to the <option> tag, for example: `<option value="" disabled="disabled">Disabled option</option>` This code subclasses the regular Django Select widget, overriding the render_option method to allow disabling options. Example of usage: class FruitForm(forms.Form): choices = (('apples', 'Apples'), ('oranges', 'Oranges'), ('bananas', {'label': 'Bananas', 'disabled': True}), # Yes, we have no bananas ('grobblefruit', 'Grobblefruit')) fruit = forms.ChoiceField(choices=choices, widget=SelectWithDisabled())

  • subclass
  • select
  • widget
Read More

Admin: return to change_list with filter and pagination applied

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying. The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination. The snippet is part of your custom Model.admin in admin.py.

  • filter
  • admin
  • pagination
  • change_list
Read More
Author: fx
  • 4
  • 8

Smart Spaceless

This 'smart_spaceless' template tag is a replacement for Django's built-in 'spaceless'. If settings.DEBUG = True, spaces will not be removed to make debugging your template code easier. When DEBUG = False, spaces will be removed. Happy coding!

  • template
  • spaceless
  • template tag
  • remove spaces
  • template tags
Read More

Share button

This template can be included by other template passing the vars page_url and title: {% with content.get_url as page_url %} {% with content.title as title %} {% include 'share.html' %} {% endwith %} {% endwith %}

  • google
  • facebook
  • twitter
  • html5
  • linkedin
  • reader
  • share
Read More

Prettify HTML body contents in HTTP response

This is an enhancement of snippet [#172](http://djangosnippets.org/snippets/172/). Here I use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) — far more easier to install through pip in a virtualenv, and possibly a bit more maintained — to format and properly indent the rendered HTML from templates. I also added a check to only tidy contents in a `DEBUG=True` environment, regarding high impact on performance while doing so in production. Last, it's compatible with Django 1.2.x.

  • templates
  • format
  • html
  • tidy
  • syntax
  • output
  • prettify
  • formatting
  • indentation
  • readability
Read More

utf8-friendly dumpdata management command (no escape symbols)

Adds `--pretty` option to django `./manage.py dumpdata` command, which produces pretty utf8 strings instead of ugly unicode-escaped shit: $ ./manage.py dumpdata app.pricingplan --indent=1 [ { "pk": 1, "model": "app.pricingplan", "fields": { "name": "\u0411\u0430\u0437\u043e\u0432\u044b\u0439", } }, { "pk": 2, "model": "app.pricingplan", "fields": { "name": "\u0425\u0443\u044f\u0437\u043e\u0432\u044b\u0439", } } ]% ./manage.py dumpdata app.pricingplan --indent=1 --pretty [ { "pk": 1, "model": "app.pricingplan", "fields": { "name": "Базовый", } }, { "pk": 2, "model": "app.pricingplan", "fields": { "name": "Хуязовый", } } ]%

  • fixtures
  • management
  • dumpdata
Read More

Automatic urls for static pages

Create in your template dir html files named example.static.html and with this snippet you can get the static page with the url /example/. If you put static file in a sub-directory, the url will be /sub-directory/example/ **Example:** `static_urls = StaticUrls()` `urlpatterns = patterns('', *static_urls.discover())` `urlpatterns += patterns('',` `(r'^admin/doc/', include('django.contrib.admindocs.urls')),` `(r'^admin/', include(admin.site.urls)),` `)`

  • urls
  • static
  • static files
  • url pattern
Read More

3110 snippets posted so far.