Login

All snippets written in Python

Snippet List

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

Better debugging mail server

Python includes (and [Django recommends](http://docs.djangoproject.com/en/dev/topics/email/?from=olddocs#testing-e-mail-sending)) a simple email debugging server which prints mail to stdout. The trouble is, unlike any half-competent mail reader, long lines are broken up, and thus long URLs don't work without modification. This snippet simply unwraps long lines (broken by "=") so long URLs can be easily copied/pasted from the terminal. Save this snippet into a file named "better.py" and execute it.

  • mail
  • debuggingserver
  • long-lines
Read More

restrict user access to modeladmin via metaclass

* Include `__metaclass__ = user_lock` to your ModelAdmin class * Add `__target__ = path_to_user_field` somewhere in the ModelAdmin * done! `__target__` is what will be used in the `filter` call. examples `'user'` or `'author'` or `'message__user'` e.t.c. The result of `__target__` is the field that is then checked against `request.user`

  • admin
  • user
  • restrict
  • model-admin
Read More

currency filter without using locale

This snippet is a combination of the existing currency snippets I found and some modifications to use your own settings without the need to have the locale installed on the system. You can define in settings.py: DECIMAL_SEPARATOR = ',' THOUSAND_SEPARATOR = '.' CURRENCY_SYMBOL = u'€' With the above settings, using `{{ 1234.30|currency }}` on a template would result in `€1.234,30`

  • template
  • filter
  • currency
Read More

Custom model field for Frame or Box

**Purpose** We often need to store x,y, width and height for a model, we can store all these values in same field by having custom field. **How to Use** Save this code in *customfields.py* and in your model >*from customfields import FrameField* >*class MyModel(models,Model)* >* frame = FrameField()* And in your in views, you can use as follows >*model = MyModel.objects.get(1)* >*print model.frame.x, model.frame.y, model.frame.width, model.frame.height*

  • field
  • custom-model-field
  • frame
Read More

KMLMiddleware

This is a slight modification from the original version at [http://djangosnippets.org/snippets/709/](http://djangosnippets.org/snippets/709/) and is a middleware designed to output the right headers for generated KML or KMZ content. In the case of KMZ content, it handles the compression of the original KML content into the KMZ format. This version applies the processing to only requests ending with a .kml or .kmz in the URL. For instance, a request with the URL **http://example.com/kml/foo.kml** or **http://example.com/foo.kmz** will get processed by this middleware.

  • google-maps
  • kml
  • kmz
  • google-earth
Read More

Bulk Insert Manager

This is a small manager that just adds a "bulk_insert" function. This is very basic, I'm basically throwing it up here because it's simple and works for my current needs. Feedback on improvements (which I know would be a ton) are very welcome. Some known "gotchas": 1. This doesn't handle relationships. If, however, you want to do one-to-one or foreignkeys you'll have to use the actual table column name ('whatever_id' typically) 2. When using this I typically make a bulk_insert call every 500 iterations or so Some improvements that I think could be good: 1. Possibly just find the fields from the first object in the objs array and leave the fields argument as optional 2. Create a bulk_insert_from_file function and use LOAD DATA INFILE for mysql and whatever else supports it

  • manager
  • bulk
  • insert
  • bulk-insert
Read More

Group sequence into rows and columns for a TABLE

Two template tag filters that can be used to create a table from a sequence. <table> {% for row in object_list|groupby_columns:3 %} <tr> {% for obj in row %} <td>{{ obj }}</td> {% endfor %} </tr> {% endfor %} </table> The example above would create a table where items read from top to bottom, left to right, in 3 columns. "Empty" cells are added to the sequence by the filter so that your rows balance.

  • filter
  • templatetag
  • table
  • groupby
Read More

Django 1.2 template tag {% IF %} with {% ELIF %} support

Adds to Django 1.2 tag `{% elif %}` {% if user.nick == "guest" %} Hello guest! {% elif user.nick == "admin" or user.is_admin %} Hello admin! {% elif user %} You are registered user {% else %} Login to site {% endif %} Snipped designed for [gaeframework.com](http://www.gaeframework.com) Inspired by snippets: [#1572](http://djangosnippets.org/snippets/1572/) and [#2243](http://djangosnippets.org/snippets/2243/)

  • template
  • if
  • elif
  • django 1.2
  • tags. tag
Read More

Custom filename example with sorl-thumbnail

usage :- put it in python path and refer to it from settings.py `THUMBNAIL_BACKEND = 'full.import.path.to.SEOThumbnailBackend'` Took me a bit to figure it out since i couldn't find an existing example code for it.

  • sorl-thumbnail
Read More

IP Authorization Decorator with IP list

Improved version of http://djangosnippets.org/snippets/2205/ Example simple code: ` from django.http import HttpResponse IP_LIST = ['192.168.101.100', '192.168.101.220', '127.0.0.1', '127.0.1.1'] @ip_auth(IP_LIST) def get_parameter(request,name): parameter = get_object_or_404(Parameter,short_name=short_name) return HttpResponse(parameter.value) `

  • IP
  • Authorization
  • Decorator
Read More

login_required decorator that doesn't redirect

A login_required decorator that wraps the login view instead of redirecting to it. This prevents your site from leaking login information with HTTP status codes as explained [here](https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information). This is the way Django's admin is protected, the difference being that it checks for is_active and is_staff instead of is_authenticated. With this decorators, users directly see a login form (no redirect), post it to LOGIN_URL and are redirected to the page they first tried to see.

  • redirect
  • login_required
Read More

Compact list_filter with counter

This is an extension of the fantastic snippet "Compact list_filter" written by **onlinehero**. Follow his instructions for the installation. My version add the number of filtered objects beside the label of the filters defined by the list_filter property.

  • admin-interface
  • compact
  • list_filter
Read More

2955 snippets posted so far.