Login

Most bookmarked snippets

Snippet List

SQL Log Middleware

This middleware will add a log of the SQL queries executed at the bottom of every page. You can (should) use BeautifulSoup to place this in a specific location. Note: If you serve non-html content, it would be wise to do a mimetype check.

  • sql
  • middleware
  • log
Read More

Sharpening images

Returns a sharpened copy of an image. Resizing down or thumbnailing your images with PIL.Image tends to make them blurry. I apply this snippet to such images in order to regain some sharpness.

  • image
  • pil
  • sharpen
  • thumbnail
Read More

Search in a model spanning relations

By popular demand an example of search in models that spans more realtions. Keep a list of Q, filter the None away, feed the rest to .filter() Credit goes to Carlo C8E Miron for the idea... cheers buddy! ;)

  • search
Read More

ajax protocol for data

Can be used for create a json format response data. Just like: {response:'ok', next:'nexturl', message:'response message', data:'returned data'} for success. {response:'fail', next:'nexturl', message:'response message', error:'error messages'} for failure. And there are some dump function: json - dump a python variable into json format string json_response - dump a python variable into json format string, and then use it create a HttpResponse object.

  • ajax
  • serialize
Read More

Bootstrap button dropdown widget (replaces forms.Select)

You need jQuery and Bootstrap 2 and bootstrap-dropdown.js. Apart from that, this should be a perfect drop-in replacement for the normal select widget. A jQuery event callback maintains a hidden input field from the user's selection. Upon loading the page, the hidden input field is set. The SelectWidgetBootstrap also contains a <noscript> tag containing the normal forms.Select widget. Example usage: class MyForm(forms.Form): group = forms.ModelChoiceField(models.Group.objects.all(), widget=SelectWidgetBootstrap(), empty_label=_(u'(no group selected)'))

  • dropdown
  • select
  • bootstrap
Read More

Easier chainability with custom QuerySets

Django allows you to specify your own ModelManager with custom methods. However, these methods are chainable. That is, if you have a method on your PersonManager caled men(), you can't do this: Person.objects.filter(birth_date__year=1978).men() Normally, this isn't a problem, however your app may be written to take advantage of the chainability of querysets. For example, you may have an API method which may return a filtered queryset. You would want to call with_counts() on an already filtered queryset. In order to overcome this, we want to override django's QuerySet class, and then make the Manager use this custom class. The only downside is that your functions will not be implemented on the manager itself, so you'd have to call `Person.objects.all().men()` instead of `Person.objects.men()`. To get around this you must also implement the methods on the Manager, which in turn call the custom QuerySet method.

  • model
  • manager
  • queryset
Read More

Jquery ajax csrf framework for Django

1. Framework to extend the jquery ajax() function to construct post requests that contain a csrf token. 2. The example view used with the framework takes JSON data and returns JSON data containing either: 3. "success" with a message and additional dictionary of JSON data to use in the page 4. "error" with an error message. 5. The ajax function framework satisfies Django's csrf requirements by injecting a csrf token into the post requests created using the function. This example is a form with ~160 fields that we wanted to help fill in customer information to automatically. 1. User calls the lookup() script from the onblur attribute of the customer_id form field by leaving the field. 2. The lookup script takes the contents of the customer_id formfield and uses the jquery ajax() function to construct a JSON post request to the "/json /?act=" url. 3. The json view takes actions as get requests. We pass the post request to the JSON url already including the get request. "/json/?act=lookup" 4. The jquery framework in the snippet includes a csrf token in the ajax request automatically. 5. The customer_id is passed as JSON to the json view lookup action and customer details are attempted to be looked up in the database. 6. If successful the request returns a JSON dictionary of customer details which are pushed into the formfields using javascript in the lookup() function. The end result is if the user fills out the customer_id field of the form first (which we suggest with tooltip overlay) the customer name and address information will populate automatically. *Credit to Guangcong Luo https://github.com/Zarel

  • json
  • jquery
  • csrf
Read More

ModelChoiceField with option groups

This is a ModelChoiceField where the choices are rendered in optiongroups (this is already posible with a normal Choicefield) For this to work properly the queryset you supply should already be ordered the way you want (i.e. by the group_by_field first, then any sub-ordering)

  • modelchoicefield
Read More

Simple Django event calendar template tag

I wanted a simple way to display events for a given month in a calendar, so I adapted some code to write a template tag. `Event` is the model class, and it needs to have a `date_and_time` field. Each event is part of a `Series`, so in the view you can filter events by which series, or display events for all known Series. [Full explanation and description of simple Django event calendar template tag](http://williamjohnbert.com/2011/06/django-event-calendar-for-a-django-beginner/).

  • calendar
  • event-calendar
  • htmlcalendar
Read More

3110 snippets posted so far.