Login

Most bookmarked snippets

Snippet List

Load template from specific app

It's an update of snippet [https://djangosnippets.org/snippets/1376/](https://djangosnippets.org/snippets/1376/) to work with Django 1.8. With this piece of code, you can override admin templates without copy or symlink files. Just write your template and extend the target.

  • template
  • loader
  • app
Read More

Cancel URL Mixin

**CancelMixin** A simple mixin to use with ```generic.CreateView``` and ```generic.UpdateView``` view form templates to effortlessly implement a "Cancel" button. This smart mixin will add a URL to your context, ```{{ cancel_url }}```, that can be used as a cancel link in your form template. If no referrer URL is provided, the cancel button will link to ```default_cancel_url```, which can be overridden by view. ** **

  • template
  • django
  • mixin
  • update
  • create
  • cbv
Read More

Dynamically adding forms to a formset. OOP version.

**What It Is** This is a JavaScript-based solution to dynamically add and remove forms in formsets and inlineformsets. It requires jQuery. Originally based on this Snippet: https://djangosnippets.org/snippets/1389/ I have done a lot of work to make it OO, and am using it in production on pages with multiple inlineformsets, and even nested inlineformsets (I call it, "Inlineformset Inception"). My hope is that the code and example are enough to show how it works. **Usage Details** In the example usage, I am using a CSS class, 'light', to make every other form have a light background color. My form placeholder is an element with an ID of 'formset-placeholder' (the default). And the form selector is a class name of 'dynamic-form' (the default). When I have time, I will create a GitHub repository with the code and completed examples.

  • django
  • javascript
  • jquery
  • formset
  • inlineformset
Read More

Post-post-syncdb and post-post-migrate

The `post_syncdb` (Django pre-1.7) and `post_migrate` (>=1.7 and South) signals are fired for every single app. What I really wanted was one signal fired after the migration or syncdb completed. There's no official one, and all the snippets were doing horribly hacky things (and wouldn't work for South, anyway). This one will work for Django syncdb, and South migrate.

  • post-migrate
  • post-syncdb
Read More

Allow multiple field sorting in a single admin table column

This snippets extends ModelAdmin in order to allow multi field sorting in admin tables. Usage example: class MyModelAdmin(MultiFieldSortableModelAdmin): list_display = ( ... 'user_full_name', ... ) def user_full_name(self, obj): return obj.user.get_full_name() user_full_name.admin_order_field = ['user__first_name', 'user__last_name']`

  • ModelAdmin
  • admin_order_field
  • multifiled sorting
Read More

LocaleMiddleware without browser language discovery

This snippet holds your Django project from automatically changing language of the page to the best fitting one by discovering the client browser language. I personally needed to show the page to the user for the first time in the default language (English), although there were some translations. User can still change the language (via session cookies). Insert this middleware BEFORE the Django's `django.middleware.locale.LocaleMiddleware` in settings.

  • middleware
  • i18n
  • locale
Read More

Seeded Randomized Querysets w/ Pagination Mixin

Mixin to support pagination when randomizing querysets. Requirements: Postgres, Django Sessions Note: This shouldn't be used on large complex datasets. It utilizes the relatively slow method of '?' randomized sorting. Use with caution. Todo: MySQL support, Support for larger datasets

  • django
  • session
  • pagination
  • random
  • postgres
  • mixin
  • postgresql
  • cbv
  • seeded
Read More

get_querystring template tag

A Django Template tag used to construct urls with current querystring parameters. This is based on some code that I've written some years ago. Enjoy.

  • template
  • templatetag
  • querystring
Read More

django-constance generic view

For several projects I am using generic views instead of django-admin, I needed a generic view for constance instead of using their django-admin based app.

  • django-constance
Read More

django form template with bootstrap

## required * `{% load trans%}`before using this snippets * Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets * Bootstrap framework

Read More

log django exceptions to file

Log Django exceptions to file. Contains snippet for both WatchedFileHandler and RotatingFileHandler handlers. Configurations mentioned are separate And only works if the DEBUG = False. Use "python manage.py runserver --no-reload" If you get file in use error.

Read More

3110 snippets posted so far.