Login

Most bookmarked snippets

Snippet List

Admin actions as buttons instead of a menu

Add this to any admin changelist and your actions drop-down will be replaced with user-friendly buttons. Why mess around with templates and subclassing admin classes when you can just mangle the page with jQuery! ;-) It also adds a 'select all' label to explain the mystery top check-box (well it was a mystery to several of my clients). The line "if ($('div.actions option:gt(0)').length<=8)" checks that there aren't more than 8 actions and falls back to the drop-down if there are. Requires jQuery to be loaded.

  • admin
  • actions
Read More

AntiSpamModelForm

A general AntiSpamModelForm using some tricks to prevent spam based on current [django.contrib.comments.forms](http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py). It uses a timestamp, a security hash and a honeypot field. See [AntiSpamForm](http://www.djangosnippets.org/snippets/1925/) too.

  • forms
  • spam
  • form
  • antispam
Read More

CSV Exporting of Model Data

This is a basic view for exporting data from models. It is designed so that you can provide the GET variables used for searching/filtering within listdisplay pages in the admin, and the code will filter the same way. It allows ouputting of model data in various formats using serializers. This should be used as a urlpattern through get_url, so that you can use the admin_view decorator to properly secure the data.

  • models
  • export
  • csv
  • data
  • reports
Read More

Enhancing template tags with "as variable" syntax

Add the decorator to an already defined templatetag that returns a Node object: @with_as def do_current_time(parser, token): ... return a_node The decorator will patch the node's render method when the "as" syntax is specified and will update the context with the new variable. The following syntaxes are available: {% current_time %} {% current_time as time %} {{ time }}

  • templates
  • templatetags
Read More

SQL Log To Console Middleware

When running the Django development server, this middleware causes all executed SQL queries to get printed out to the console. This is based on [Snippet 161](http://www.djangosnippets.org/snippets/161/). The big difference is that 161 logs by adding stuff to your response text, which isn't very convenient IMO.

  • sql
  • middleware
  • log
  • debug
  • logging
  • console
Read More

Append paramaters to a GET querystring (template tag)

This tag is designed to facilitate pagination in the case where both the page number and other parameters (eg. search criteria) are passed via GET. It takes one argument - a dictionary of GET variables to be added to the current url Example usage: {% for page_num in results.paginator.page_range %} <a href="{% append_to_get p=page_num %}">{{ page_num }}</a> {% endfor %} Note that the passed arguments are evaluated within the template context.

  • get
  • pagination
  • request
Read More

TRAC-Ticket on exception

This is a small approach to have a middleware which automatically creates a ticket in an existing Trac environment. **Note:** you must have the [XML-RPC-Plugin](http://trac-hacks.org/wiki/XmlRpcPlugin) installed. Extend the attrs-dict to your needs. For example: in my case I have the SensitiveTicket-Plugin installed - automatically created tickets are marked as sensitive and are not visible to the public.

  • middleware
  • exception
  • trac
  • ticket
Read More

invalidation of cache-template-tag cache

this function invalidates a template-fragment cache bit. say you have: {% load cache %} {% cache 600 user_cache user.id %} something expensive here {% endcache %} maybe you want to force an update. With this function you can, just call: invalidate_template_cache("user_cache", user.id)

  • template
  • cache
  • invalidate
Read More

CountryField (ISO 3166-1)

List of countries based on the ISO 3166-1 standard. List adapated from http://opencountrycodes.appspot.com/python/ This is useful for certain services such as Protx that requires countries in the two letter standard.

  • field
  • country
  • custom-field
  • iso-3166-1
  • protx
Read More

twitterize filter

This filter links twitter user names prefixed with '@', hash tags, and URLs. Usage example: `{{var|twitterize}}` Note: Do not use this in conjunction with the urlize filter. Twitterize does this for you.

  • filter
  • twitter
  • hash-tags
  • @replies
Read More

Debug view: show available named URL patterns

Hook the show_url_patterns view function in to your URLconf to get a page which simply lists all of the named URL patterns in your system - useful for if your template developers need a quick reference as to what patterns they can use in the {% url %} tag.

  • urlconf
  • debug
  • debugging
  • urlpatterns
  • documentation
  • docs
Read More

Queryset Foreach

Call a function for each element in a queryset (actually, any list). Features: * stable memory usage (thanks to Django paginators) * progress indicators * wraps batches in transactions * can take managers or even models (e.g., `Assertion.objects`) * warns about `DEBUG`. * handles failures of single items without dying in general. * stable even if items are added or removed during processing (gets a list of ids at the start) Returns a `Status` object, with the following interesting attributes * `total`: number of items in the queryset * `num_successful`: count of successful items * `failed_ids`: list of ids of items that failed

  • queryset
  • progress
  • callback
  • map
  • foreach
  • iterator
Read More

3110 snippets posted so far.