Login

Most bookmarked snippets

Snippet List

Delicious Tag

Just add it in templatetags/delicious.py In your template: <h3>Del.icio.us</h3> <ul class="list"> {% load delicious %} {% load_delicious_links %} {% for link in delicious_links %} <li><a href="{{link.link}}">{{link.title|safe}}</a></li> {% endfor %} </ul>

  • tag
  • delicious
Read More

JSON encoding middleware

Makes it really easy to return JSON from your views: just return a dict. (Also from [django-webapp](http://code.google.com/p/django-webapp/).)

  • middleware
  • serialize
  • json
  • encode
  • encoding
  • serializer
Read More

Ajax error handling

Handles exceptions from AJAX code, giving more useful errors and tracebacks. (By the way, all these new snippets are extracts from [django-webapp](http://code.google.com/p/django-webapp/).) This exact code is not well tested, but it's refactored from some code we use in production.

  • middleware
  • ajax
  • error
  • exception
Read More

EditInline for GenericForeignKey

A simple InlineModelAdmin class that enables you to edit models that are bound by the instance via a generic foreign key (`content_type`, `object_id` pair) Use like: class PlacementInlineOptions( generic.GenericTabularInline ): model = Placement extra = 2 ct_field_name = 'target_ct' id_field_name = 'target_id' Can be also found at #4667

  • admin
  • foreignkey
  • generic
  • edit-inline
Read More

Admin change language

This snippet adds a select language drop down menu in the admin bar (just after Documentation link). For this approach it's necessary to copy from admin templates the admin/base.html to the project templates (keeping the admin directory), changing just line 25 ad shown. Then it's necessary to create specified admin/change_language.html template.

  • admin
  • language
  • change
Read More

Protect anti robots template tag

This code works like that in the Google Groups you see and when try to see an e-mail and it is like this "[email protected]" with a link to write a captcha code and see the true value. You can use it for anything you want: links, blocks of texts, block of HTML, etc. To use in your template, place the a code like this (remember to load the template tags file with a {% load file_name %} before): {% protectantirobots %} <a href="mailto: {{ office.email }}">{{ office.email }}</a> {% endprotectantirobots %} You can also use **django-plus** application to do this: [http://code.google.com/p/django-plus/](http://code.google.com/p/django-plus/)

  • captcha
  • security
  • protect
  • anti
  • robots
  • safe
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

JSON-compatible query filter specification

This function is designed to make it easier to specify client-side query filtering options using JSON. Django has a great set of query operators as part of its database API. However, there's no way I know of to specify them in a way that's serializable, which means they can't be created on the client side or stored. `build_query_filter_from_spec()` is a function that solves this problem by describing query filters using a vaguely LISP-like syntax. Query filters consist of lists with the filter operator name first, and arguments following. Complicated query filters can be composed by nesting descriptions. Read the doc string for more information. To use this function in an AJAX application, construct a filter description in JavaScript on the client, serialize it to JSON, and send it over the wire using POST. On the server side, do something like: > `from django.utils import simplejson` > `filterString = request.POST.get('filter', '[]')` > `filterSpec = simplejson.loads(filterString)` > `q = build_query_filter_from_spec(filterSpec)` > `result = Thing.objects.filter(q)` You could also use this technique to serialize/marshall a query and store it in a database.

  • filter
  • ajax
  • json
  • database
  • query
Read More

Log errors to a file

This is another example use of the [exception middleware](http://www.djangosnippets.org/snippets/638/). It shows how to log exceptions to a file. Someone wanted to do this to avoid DOS-ing the email server in case of a silly error. (untested.)

  • middleware
  • log
  • error
  • exception
  • file
Read More

Search djangosnippets.org

I was tired browsing via tag to find snippets I saw a while ago. So I created a custom search engine with Google. To try it out go to [http://henning.cco-ev.de/django/djangosnippets.html](http://henning.cco-ev.de/django/djangosnippets.html)

  • snippets
  • search
  • google
  • snippet
  • djangosnippets
  • cse
Read More

Boxes as template tags

It's a template tag used to create boxes with nested divs (useful to keep the templates DRY). For example: {% menubox titlevar %} Content here {% endmenubox %} will generate the html with the nested divs div class=box div class=box-outer div class=box-inner Headline Content /div /div /div [more detail on this blog post](http://pedro.valelima.com/blog/2007/sep/26/boxes-template-tags/)

  • template-tag
  • box
Read More

django_template decorator

Easy way to caching template. Very simple usage: @django_template("my_site.html") def master_home(request): variables = { 'title' : "Hello World!" } return variables

  • template
  • render
  • decorator
Read More

SplitTimeField

The SplitTimeField and the corresponding widget SplitDateTimeWidget show two select boxes with one for hour from 0 to 23 and the other showing minutes 0,15,30 and 45 (can be customized very easily). Usage: ------- class TestForm(forms.Form): start_time = SplitTimeField(widget=SplitTimeWidget) end_time = SplitTimeField(widget=SplitTimeWidget)

  • forms
  • field
  • widget
Read More

3110 snippets posted so far.