Login

Most bookmarked snippets

Snippet List

A Django form field that accepts an uploaded image and creates a resized image attached with a push-pin

PushPinImageField is a Django form field that is a sub-class of an ImageField. The field accepts an image to upload and based on certain settings, notably the size of the resulting image, the sizes and colors of 3 different borders, as well as the color of the push pin, a re-sized image is created with a colorized push pin in the top-center. A live demo of the field as well as more detailed usage instructions are available as a [blog entry](http://www.robmisio.com/blog/2/).

  • django
  • image
  • thumbnail
  • form
  • field
  • custom-field
Read More

Yet another query string template tag

This one works works with or without query string dicts defined in the context. And it handles replacement, addition and removal of values for parameters with multiple values. Usage: {% url view %}{% query_string qs tag+tags month=m %} where `view`, `qs` (dict), `tags` (list of strings) and `m` (number) are defined in the context. Full detail in the doc string.

  • url
  • template-tag
  • query-string
Read More

Admin action for a generic "CSV Export"

Based on [#2020](/snippets/2020/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])] * - Added UTF-8 encoding support * - Bugs fixed

  • admin
  • export
  • csv
  • action
Read More

Keep Me Logged In for Django

Very simple middleware to implement "remember me" functionality. Updates the session once per day to keep user logged.

  • django
  • session
  • keep
  • logged
  • middlware
Read More

Template Tag of Django Image Thumb Creator

**You can save these codes into a templatetag file in your django app. Then use codes like these in your template files:** **************************************************************************** {% load *yourtags* %} ... <img src="{% thumb *yourmodel.picturefield* 200 300 0 %}"/> <img src="{% thumb *yourmodel.picturefield* 500 400 yes %}"/> ... The parameters are: imagefield ImageField width Integer height Integer rescale [1, yes, true, 0, no, false] **Some codes come from <djangosnippets.org>**

  • django
  • image
  • templatetag
  • thumb
Read More

Improve ajax progress bar by jquery extensions

This snippet in the one to improve the method described in [http://djangosnippets.org/snippets/679/](http://djangosnippets.org/snippets/679/). It uses some jquery extensions to make the task more easier: **jquery.timers.js**, **jquery.progressbar.js**, **jquery.form.js**.

  • ajax
  • javascript
  • jquery
  • upload
  • file
  • progress
  • jquery extension
Read More

CSV serializer

CSV serialization for models. Can be used via the dumpdata/loaddata management commands or programmatically using the django.core.serializers module. Supports multiple header lines and natural keys. Add the following to settings.py: SERIALIZATION_MODULES = { 'csv' : 'path.to.csv_serializer', } Examples of usage: $ python manage.py dumpdata --format csv auth.user > users.csv from django.core import serializers csvdata = serializers.serialize('csv', Foo.objects.all()) To run the regression tests distributed with the Django tarball: $ cd /path/to/Django-1.2.x/tests $ PYTHONPATH=/path/to/myproject ./runtests.py --settings=myproject.settings serializers_regress

  • csv
  • serializer
Read More

Management command decorator

A quick-and-dirty, and extremely simple, decorator to turn a simple function into a management command. This still requires you to have the management directory structure, but allows you to name your primary method whatever you want, and encapsulates the basic functionality of an argument-accepting management commmand. The function's docstring will be used for the command's help text if the `help` arg is not passed to the decorator. Simple usage: from myapp.utils import command @command() def my_command(): print "Hello, world" I'm not too familiar with the intricacies of decorators and management commands, so this could probably (most likely) be improved upon, but it's a start. **Update**: I've taken this a bit farther and put my work up on bitbucket: https://bitbucket.org/eternicode/django-management-decorators/src

  • decorator
  • management
  • command
Read More

"Autoconnect" model decorator, easy pre_save and post_save signal connection

This method allows you to define pre_save and post_save signal connections for your decorators in a little more clean way. Instead of calling `pre_save.connect(some_func, sender=MyModel)`, or perhaps `pre_save.connect(MyModel.some_static_func, sender=MyModel)`, you can simply define the pre_save method right on your model. The @autoconnect decorator will look for pre_save and post_save methods, and will convert them to static methods, with "self" being the instance of the model.

  • pre_save
  • post_save
  • signal-connect
Read More

JSON View Decorator

Django JSON view decorator. Dumps the object returned from the view function. Allows you to customize the JSON dumps() function using the available keyword arguments available from the simplejson module. By default, indents the output with 4 characters.

  • json
  • decorator
Read More

Middleware to move tags <script> to the bottom

This middleware makes the page load faster because it moves all tags <script> to the end of document. When a tag <script> loads, it blocks parallel loading, so, the best practice is load them after load CSS and images. To use it, just put in a file and add to your setting MIDDLEWARE_CLASSES.

  • performance
  • script
  • otimization
Read More

Add a CSS class to every field indicating what kind of field it is

The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature. See also: [James Bennett's explanation of formfield_callback](http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171)

  • css
  • field
  • widget
  • modelform
  • callback
Read More

django subdomain support for both resolve and reverse.

Add these two middleware to the top of MIDDLEWARE_CLASSES. Add BASE_DOMAIN to your setting file : BASE_DOMAIN = '.13.com'. your root urlconf may like this: urlpatterns = patterns('', url(r'^www:(?P<id>[0-9]+)/$', 'couponcn.store.views.site_index', name='site_index'), url(r'^news:abc/def/$', 'couponcn.store.views.site_index', name='site_index2'), ) then {% url site_index id=4 %}<br /> {% url site_index2 %} in your template or the reverse function will work out urls like this: http://www.13.com/4/ http://news.13.com/abc/def/

  • url
  • reverse
  • resolve
  • subdomain
Read More

Limit queryset to objects related to parent in ManyToMany fields within admin inlines

`formfield_for_manytomany` allows you to limit the choices/queryset for a ManyToManyField, but without direct access to the parent object. This snippet stores a reference to the parent object in `get_formset` and allows limiting of `ManyToManyField`s to objects related to the same parent object. See `ExampleInline` for example usage. If for some reason you have a `ManyToManyField` in a `TabularInline`, just change the `template` in the subclass.

  • limit_choices_to ManyToMany ManyToManyField admin inline formfield_for_manytomany
Read More

3110 snippets posted so far.