Login

Most bookmarked snippets

Snippet List

DRY template rendering decorator

in this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps. Regards, Paul

  • template
  • rendering
  • dry
Read More

mkrange - create a range() inside a template

Accepts the same arguments as the 'range' builtin and creates a list containing the result of 'range'. Syntax: {% mkrange [start,] stop[, step] as context_name %} For example: {% mkrange 5 10 2 as some_range %} {% for i in some_range %} {{ i }}: Something I want to repeat\n {% endfor %} Produces: 5: Something I want to repeat 7: Something I want to repeat 9: Something I want to repeat

  • templatetag
  • range
Read More

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

Declaring django views like web.py views

I work a little with [web.py framework](http://webpy.org/) and I like a lot the view definition. For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if http request is a POST request the POST method is called. Then you can define common stuff in another method that could be called inside each method, and you have a class for each view.

  • views
  • class
  • web.py
Read More

Flatpage Suggester Template tag for 404 templates

This template tag finds FlatPages with urls 'similar' to the given request_path. It takes the request_path from the page_not_found view (django.views.defaults), picks it apart, and attempts to match existing FlatPages that have a 'similar' URL. For example, if the URL that resulted in a 404 was: /foo/bar/baz/whatever/ This tag would look for FlatPages whose URL starts with the following: /foo/bar/baz/whatever/ /foo/bar/baz/ /foo/bar/ /foo/

  • template-tag
  • flatpage
  • 404
  • suggestion
Read More

SelfForeignKey to prevent hierarchical loops

When you have a model containing a field that is a foreign key back to the same model, you could find yourself with a hierarchy with an infinite loop: #Data modelling Back to the Future > grandfather > father > son > father > ... Using this field instead of the standard ForeignKey will ensure that no model instance is saved that has itself as an ancestor, breaking the relationship if it does. (Enhancements: I am sure one would want to better enhance this with appropriate error handling instead of silently disconnecting the relationship. And the relevant forms ought not show ancestors in the field's widget to reduce the chances of this happening in the first place.)

  • pre_save
  • recursion
  • loop
  • foreign
Read More

Remaining character count in django admin

Want to display the remaining characters on a text field in admin? (based off of maxlength or an override) also lives here: [http://github.com/broderboy/django-admin-remainingcharacters](http://github.com/broderboy/django-admin-remainingcharacters)

  • admin
  • jqeury
  • charactercount
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

Model Choices Helper

This is my attempt at a convenience class for Django model choices which will use a [Small]IntegerField for storage. It's very similar to [jacobian's version](http://www.djangosnippets.org/snippets/1664/), but I wanted to be able to use simple attributes for access to the integer values. It's not technically dependent on Django, but it's probably not a datatype that would be useful for much else. Feel free to do so however if you have a use-case. >>> statuses = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) >>> statuses.live 0 >>> statuses.hidden 2 >>> statuses.get_choices() ((0, 'Live'), (1, 'Draft'), (2, 'Not Live')) This is then useful for use in a model field with the choices attribute. >>> from django.db import models >>> class Entry(models.Model): ... STATUSES = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) ... status = models.SmallIntegerField(choices=STATUSES.get_choices(), ... default=STATUSES.live) It's also useful later when you need to filter by your choices. >>> live_entries = Entry.objects.filter(status=Entries.STATUSES.live)

  • django
  • models
  • choices
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

3110 snippets posted so far.