Login

All snippets written in Python

2956 snippets

Snippet List

Percent Field

This can be used in a forms.Form or forms.ModelForm in django. Render a decimal field between 0 and 1 into a value between 0 and 100, and vice-versa.

  • field
  • percent
Read More

Foreign Key list_filter wthout custom FilterSpec

This is some (probably) pretty dodgy code that allows for foreign keys in the admin's list_filter without using a custom FilterSpec. It overrides the django.db.models.options.Options get_field class to handle spanned relationships, i.e. list_filter=('house__room__town',) as seen in http://code.djangoproject.com/ticket/3400 I have only tested this with a double foreign key relationship('house__room'), and it worked. It hasn't been tested in production, and the troubling part of this code is probably to use of 'deepcopy'. I do add those copies back to the options instance, so there shouldn't be too much runaway copying.

  • foreign-key
  • filterspec
  • list-filter
  • 3400
Read More

Unzip a .zip file uploaded with FileBrowser

Django Filebrowser (http://code.google.com/p/django-filebrowser/) provides a signal called filebrowser_post_upload. Adding this method to a model that has a FileBrowse field will cause uploaded .zip files to be detected and unzipped on the server in place, then deleted (leaving their folder behind). It will also delete the garbage __MACOSX folder created by Mac zip files. This is probably not safe to do for publicly uploaded files. The use case here was to allow journalists to upload entire SoundSlides projects into a custom CMS.

  • filebrowser
  • zip
  • unzip
Read More

Updated Filter to resize a ImageField on demand (ver.2)

An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model

  • image
  • templatetag
  • thumbnail
  • resize
  • imagefield
Read More

Fieldsets for Views

This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin. How to Use: In the view in question, put: def some_view(request): ... fieldsets = ( (u'Title 1', {'hidden' : ('field_1', 'field_2',), 'fields' : ('field_3',)}), (u'Title 2', {'hidden' : ('field_5', 'field_6',), 'fields' : ('field_4',)}),) ) return render_to_response('some.html', {'fieldsets': fieldsets}) fieldsets = ( (None, {'hidden' : ('evento', 'colaborador',), 'fields' : ('acompanhantes',)}), ) Next, in the html just add: <form enctype="multipart/form-data" id="edit" method="post" ...> ... {% include "inc/form_snippet.html" %} ... <input type="submit" value="Submit"> </form>

  • template
  • django
  • admin
  • views
  • filters
  • python
  • tags
  • html
  • css
  • dicts
Read More
Author: Nad
  • 2
  • 0

Remember me for login

Set session to never expire in settings, and when remember_me is found false in login POST, set it to browser session expiry. Works only in Django 1+.

  • views
  • python
  • login
  • rememberme
Read More

HibernateBooleanField

Sometimes its necessary to map your django models to Java Hibernate created tables. Hibernate maps boolean field to bit(1) column instead of tinyint in django. NOTE: tested for mysql backend only

  • model
  • field
  • hibername
Read More

Choices

This allows you to access the choices (and their respective values) you create as a dictionary. It works great within django and it allows you to reference the choices as a dictionary (CHOICES[CHOICE1]) instead of CHOICES[0][0]... it is a tuple... but I mean, come on... what if you change the order? If you need the tuple just call CHOICES.choices and it will return the standard tuple.

  • choice
  • choices
  • input
Read More

Button Admin

This is an improvement on other ButtonAdmin implementations. A few features : It allows you to specify whether button appears on change form or change list; whether the form is to be saved before the resulting function is called; whether the button should be displayed Look at the bottom of the snippet for usage. And make sure you admin urls are enabled using (r'^admin/', include(admin.site.urls)) instead of the old method

  • admin
  • button
Read More

Map GPX files to 3D GeoDjango Models

`GPXMapping` is a subclass of `LayerMapping` that imports GPX files into 3D GeoDjango models (requires Django 1.2 or SVN r11742 and higher). Here's an example of GeoDjango models for GPX points and tracks, respectively: from django.contrib.gis.db import models class GPXPoint(models.Model): timestamp = models.DateTimeField() point = models.PointField(dim=3) objects = models.GeoManager() def __unicode__(self): return unicode(self.timestamp) class GPXTrack(models.Model): track = models.MultiLineStringField(dim=3) objects = models.GeoManager() Assuming the above models, then `GPXMapping` may be used to load GPX tracks and waypoints (including elevation Z values): track_point_mapping = {'timestamp' : 'time', 'point' : 'POINT', } track_mapping = {'track' : 'MULTILINESTRING'} gpx_file = '/path/to/file.gpx' lm = GPXMapping(GPXPoint, gpx_file, track_point_mapping, layer='track_points') lm.save(verbose=True) lm = GPXMapping(GPXTrack, gpx_file, track_mapping, layer='tracks') lm.save(verbose=True)

  • gis
  • geodjango
  • 3d
  • gpx
  • layermapping
Read More

Simple views dispatcher by http methods

Calls a view by request.method value. To use this dispatcher write your urls.py like this: urlpatterns = pattern('', url(r'^foo/$', dispatch(head=callable1, get=callable2, delete=callable3)), ) If `request.method` is equal to head, `callable1` will be called as your usual view function; if it is `get`, `callable2` will be called; et cetera. If the method specified in request.method is not one handled by `dispatch(..)`, `HttpResponseNotAllowed` is returned.

  • urls
  • rest
  • http
  • url
Read More