Login

Most bookmarked snippets

Snippet List

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

DisableableSelectWidget

A Select widget that allows choices to be disabled. Specify `disabled_choices` to indicate which choices should be present in the list, but disabled. A possible use case for this is a form that displays data that can be edited by privileged user's but only viewed by others.

  • form
  • select
  • widget
Read More

Drupal password hasher for migration

This BasePasswordHasher allows the easy migration of passwords from Drupal to Django 1.4. Drupal stores its passwords using a SHA512 hash, but with some iterations and postprocessing. This snippet allows you to migrate the username and passwords over seamlessly- the only necessary change is truncating the first character of each password hash (since Django 1.4 stores each password as algorithm$hash). Note that this snippet *requires* Django 1.4, but there is no option for that snippet in the list. Provided as a github gist [here](https://gist.github.com/2344345).

  • migration
  • password
  • hash
  • drupal
Read More

Handles Inline Formsets and also "in-standard-way" normal forms

If you read the docstring and the example you should get a clue what this Code does. I didn't want a big function everytime that handles every specific form, formset combinations so this how i can add/edit Models with specific Forms given to the magic_handle_inlineformsets function. It also works for Forms without innline_formsets.

  • forms
  • formset
  • inline
  • inlineformset
  • inline_formset
Read More

Admin Download as CSV File

This function downloads selected rows to CSV file. The snippet is based on snippet 1697 and 2020. This function downloads all columns given in the list_display, including callable items.

  • admin
  • csv
  • export csv
Read More

Async PIL resize of images

Call resize_image to replace the image with a resized and normalized version of itself. I recommend doing this with celery, but you could also hook it up to the admin interface if you're not impatient.

  • Images
  • PIL
Read More

Dump a model instance and related objects as a Python data structure

This utility makes a text dump of a model instance, including objects related by a forward or reverse foreign key. The result is a hierarchical data structure where * each instance is represented as a list of fields, * each field as a (<name>, <value>) tuple, * each <value> as a primitive type, a related object (as a list of fields), or a list of related objects. See the docstring for examples. We used this to make text dumps of parts of the database before and after running a batch job. The format was more useful than stock `dumpdata` output since all related data is included with each object. These dumps lend themselves particularly well for comparison with a visual diff tool like [Meld](http://meldmerge.org/).

  • serialize
  • dump
  • model
  • instance
Read More

3110 snippets posted so far.