Login

All snippets written in Python

Snippet List

Multiple inheritance of newforms and modelforms

If you try to use multiple inheritance with a modelform (to mix in some fields from an already existing form class for example) you'll get the following rather terrifying error: > "Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases" The solution is to first create the ModelForm, then create a NEW class that inherits from both the ModelForm and the form you want to mixin, then finally apply the recipe from here: [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197)

  • newforms
  • inheritance
  • modelforms
  • multipleinheritance
  • metaclasses
  • metaclass
Read More

sharedance sessions backend

"Sharedance is a high-performance server that centralize ephemeral key/data pairs on remote hosts, without the overhead and the complexity of an SQL database." [Frank DENIS](http://sharedance.pureftpd.org/project/sharedance)

  • session
  • backend
Read More
Author: d2
  • 0
  • 0

Preview tag for fields with choices

django.contrib.formtools in preview displaying only field.data by default. Its not convenient to see integer value for fields with radio buttons or select choices. This custom tag trying to show string value from choices if available.

  • template
  • tag
  • newforms
  • preview
Read More

Auto HTML Linebreak filter

This custom filter is helpful if you want to convert plain text to include html line breaks but you aren't sure if the text is actually plain text or if it already contains html line breaks. First the filter looks for if the text contains any br, p, or table tags, if it does the text is returned as is. If it doesn't then the same functionality as the [linebreaks](http://www.djangoproject.com/documentation/templates/#linebreaks) filter is applied to the text.

  • filter
  • html
  • linebreaks
  • custom-filter
Read More

Update ContentTypes and Permissions without syncdb

[See blog post](http://paltman.com/2008/04/11/keeping-contenttypes-and-permissions-updated-without-syncdb/) You can put this script in the root of your project and run after deploying updates in your production environment.

  • sql
  • permissions
  • deploy
  • contenttypes
Read More

XhtmlMortifierMiddleware

This middleware checks for xhtml mimetypes if the browser supports a "application/xhtml+xml" response. If not, it converts the response headers to "text/html". To enable this middleware add it the the MIDDLEWARE_CLASSES setting and make sure it appears somewhere after GZipMiddleware, so that it's processed first.

  • middleware
  • xhtml
  • user-agent
Read More

Customizable newforms labels with a template tag

This template tag and suggested template allow greater control over rendering `<label>` tags for your newforms fields than using `field.label_tag`. I save the provided Python code in my app as `templatetags/forms.py` (although this name may conflict in the future). The simplest usage: {% label field %} One use case is adding `class="required"` to the label tag for required fields instead of inserting markup elsewhere--this is done in the given example template. Alternate label text and tag attributes can be passed to the inclusion tag: {% label field "Alt. label" 'class=one,id=mylabel' %}

  • templatetag
  • newforms
  • label
Read More

newforms-admin edit callback-url hook

NOTE: this is for **newforms-admin** I need edit links for items on my site, outside of the django admin -- however, I'm lazy, and don't want to build my own edit forms when there's a perfectly nice admin already there. Trick is, I need to be able to click a link to the django admin edit page for an item, and have it return to the calling page after saving. This chunk of code does the trick (the "real" version extra cruft, naturally) -- the links will bring up the django admin editor, then return to the calling page after saving.

  • newforms
  • admin
  • url
  • newforms-admin
  • newformsadmin
  • edit
  • callback
  • hook
Read More

Unique Slugify

Automatically create a unique slug for a model. Note that you *don't* need to do `obj.slug = ...` since this method updates the instance's slug field directly. All you usually need is: `unique_slugify(obj, obj.title)` A frequent usage pattern is to override the `save` method of a model and call `unique_slugify` before the `super(...).save()` call.

  • slug
Read More

Render to file

As a demo, I was asked to write a `render_to_file()` function to load a template and render it to a file. Turns out it's amazingly easy, and I think it's a neat trick to have in your bag of tools.

  • template
  • render
  • file
Read More

Resize image on save

This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save). In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.

  • image
  • pil
  • resize
Read More

2956 snippets posted so far.