Login

All snippets written in Python

Snippet List

Image model with thumbnail

A relatively simple Photo model which generates its own thumbnail when saved. A default size is specified as `thumb_size` in the `save()` arguments. Other thumbnailing strategies don't save the thumbnail dimensions, and since the actual dimensions of the thumbnail created by PIL's `thumbnail` method are somewhat non-deterministic, it is difficult to create an `img` tag with proper height and width attributes. This approach makes that task simple. This approach works well if only one thumbnail size is required. It could easily be adapted to support two or three thumbnail sizes, but adding more sizes would quickly get unwieldy. This was adapted from http://biohackers.net/wiki/Django1.0/Thumbnail

  • image
  • thumbnails
  • thumbs
Read More

Continuing and breaking from loops in Django templates

This snippet makes Django templates support `break` and `continue` in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost. `break` and `continue` are implemented as template filters, with the input value being the loop variable. For example, to break from the current `for` loop use `forloop|break`, and to continue from the next outer loop use `forloop.parentloop|continue`. The implementation monkeypatches Django (specifically Nodelist and ForNode) and has been tested on v1.2 with Python 2.6.

  • template
  • loop
  • break
  • continue
Read More

Get actual child model in multi-table inheritance

A common problem (it hit the Django mailinglist a couple of times) is that if you get `models.Topping.objects.all()`, you get a list of toppings, although they stand for other classes such as `SalamiTopping` or `CheeseTopping`. If you need the actual object, just derive `Topping` from `PolymorphicModel`, and say `topping.actual_instance`. This will give you e.g. a `SalamiTopping`. Sometimes you just want to check for the actual class. You can get it by saying `topping.content_type.model_class()`. There is a slight performance impact when creating objects because they have to be saved twice. NEWS: A good alternative to this approach is the [InheritanceManager](https://github.com/carljm/django-model-utils/blob/master/README.rst).

  • models
  • child-model
  • multi-table-inheritance
  • polymorphy
Read More

Use MEDIA_URL in flatpages with SSL

This is a copy of [snippet 654](http://djangosnippets.org/snippets/654/), modified to allow dynamic MEDIA_URL, as you might need that for SSL in combination with [snippet 1754](http://djangosnippets.org/snippets/1754/). This is a template filter to enable the use of the MEDIA_URL setting in content from the flatpages database table. It searches for {{ MEDIA_URL }} and replaces it with the current MEDIA_URL added by a context processor. Note: To set up, drop the above code into a file called media_url.py in your templatetags directory in one of your INSTALLED_APPS, and add the filter to your flatpages template like so: {% load media_url %} {{ flatpage.content|media_url:MEDIA_URL }}

  • filter
  • ssl
  • context
  • flatpages
  • processor
Read More

Number generator to autofill a field

**Number format:** number/year **Purpose:** When the user adds a new bill, the "number" field will be automatically filled with the appropriate number. * The snippet automatically increments the bill number based in the highest number available. This is because in the use case, the user could start by *any* number, and we couldn't use the PK number (p.e. if the user first object number is 324/10, using the PK will convert it to 1/10) **NOTE:** bill_type is a Boolean value to mark if the bill is outgoing or ingoing. Since we only need to mark the outgoing ones, we filter through this field.

  • django-admin
  • number-generator
  • autofill
Read More

Managing Google AppEngine datastore maintenance

This code defines a decorator that inform users of your site when the Google AppEngine data store is in read-only mode during maintenance periods. Use it as a decorator on your views that require write access to the data store. @requires_datastore_write def update(request): ... Create a `maintenance.html` Django template (or change the name in the code) with the message that the user will see, something like: This application is currently in maintenance mode and some operations are temporarily unavailable. Thanks for trying back later. Sorry for the inconvenience.

  • decorator
  • readonly
  • appengine
  • maintenance
  • capabilities
  • datastore
Read More

NoneWidget

Sometime may be useful to disable the HTML output from formfield to template.

  • widgets
  • widget
Read More

Combine ProfileForm an UserForm in one.

Using this method you can combine form for standart django.contrib.auth.models.User model and for your project profile model. As now, ProfileForm can be used as usual, and it will also contain UserForm fields.

  • forms
  • user
  • profile
Read More

Select Dropdown Widget with jQueryUI

This widget can be imported in your forms.py file and used like so: formfield = forms.ModelChoiceField(widget=SelectDropdownWidget, queryset=Model.objects.all()) The javascript is provided by [filament group's Scott and his jQueryUI Selectmenu](http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/). In addition to the javascript you will need [jQuery (the latest release is fine) and jQuery UI](http://jqueryui.com/). The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable! Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)

  • dropdown
  • jquery
  • select
  • widget
  • jqueryui
Read More

Multiple Select Widget with jQueryUI

This widget can be imported in your forms.py file and used like so: formfield = forms.ModelMultipleChoiceField(widget=MultiSelectWidget, queryset=Model.objects.all()) The javascript is provided by [Michael's Multiselect](http://github.com/michael/multiselect/tree/next). In addition to the javascript you will need [jQuery (the latest release is fine) and jQuery UI](http://jqueryui.com/). The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable! Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)

  • jquery
  • widget
  • multiple select
  • multiselect
  • jqueryui
Read More

In-memory XML-RPC server based on URL

This is a XML-RPC server, that uses arguments in URLs and every dispatcher instance is prepared in memory during webserver run. It's good, for example, for securing XML-RPC server with hashed strings and there are a lot of similar use cases. Usage: from xmlrpclib import ServerProxy server = ServerProxy('http://example.com/xmlr-rpc/%s/' % something, allow_none=True) server.do_something(*args)

  • xml
  • server
  • dispatcher
  • xmlrpc
Read More

2955 snippets posted so far.