Login

All snippets written in Python

Snippet List

TestCase base class to easily temporarily change module values

1. Base your test case off `ModuleTestCase` and set a class attribute containing a dictionary of modules which you want to be able to revert the values of. 2. Use `self.modulename.attribute = something` in your `setUp` method or test cases to change the module's attribute values. 3. The values will be automatically restored when each test case finishes. For the common case of reverting the settings module, just use the `SettingsTestCase` as your base class.

  • tests
Read More

Template filter to turn Twitter names into links

Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites. Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).

  • twitter
  • twtterize
Read More

Add GET parameter tag

This is custom tag I wrote for myself for solving situations when you have filter form and page numbers in the same page. You want to change ?page=.. or add it if it doesn't exist to save filter form data while moving through pages. Usage: place this code in your application_dir/templatetags/add_url_parameter.py In template: {% load add_get_parameter %} <a href="{% add_get_paramater param1='const_value',param2=variable_in_context %}"> Link with modified params </a> It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS URL: [http://django.mar.lt/2010/07/add-get-parameter-tag.html](http://django.mar.lt/2010/07/add-get-parameter-tag.html)

  • tag
  • parameters
  • GET
  • add
  • add_get_parameter
Read More

Partial Tag

Lets you include another template and pass in named variables. Use like: {% partial field_template field=form.city %} If no key is specified, it will use "item" instead. You may pass in multiple variables as comma-separated key=value pairs.

  • context
  • partial
  • include
  • ssi
Read More

View Redirect Decorators

Temporary and permanent view redirect decorators. Simplifies views that always redirect to a specific or configured url. You can use the reverse() function (or any other runtime-required lookup) via `lambda` to define the redirect url.

  • view
  • decorator
  • redirects
Read More

JSON View Decorator

Django JSON view decorator. Dumps the object returned from the view function. Allows you to customize the JSON dumps() function using the available keyword arguments available from the simplejson module. By default, indents the output with 4 characters.

  • json
  • decorator
Read More

Middleware to move tags <script> to the bottom

This middleware makes the page load faster because it moves all tags <script> to the end of document. When a tag <script> loads, it blocks parallel loading, so, the best practice is load them after load CSS and images. To use it, just put in a file and add to your setting MIDDLEWARE_CLASSES.

  • performance
  • script
  • otimization
Read More

Aggregation class "Count" with Case

Use it like below: totals = MyClass.aggregate( is_enabled_yes=CountCase('is_enabled', when=True), is_enabled_no=CountCase('is_enabled', when=False), count_if=CountCase('id', case="another_field in ('a','b')", when=True), )

  • count
  • annotate
  • aggregation
Read More

Aggregation class "Sum" with Case

Just use it like below: from downloaded_file import SumCase MyClass.objects.aggregate( sum1=SumCase('salary', case='salary < 4', when=True), sum1=SumCase('salary', case='type', when='director'), )

  • annotate
  • aggregation
  • sum
Read More

Add a CSS class to every field indicating what kind of field it is

The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature. See also: [James Bennett's explanation of formfield_callback](http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171)

  • css
  • field
  • widget
  • modelform
  • callback
Read More

While loop template tag

The missing `while` template tag. Built on top of http://djangosnippets.org/snippets/2093/, it also supports `break` and `continue` out of the box.

  • template
  • templatetag
  • loop
  • while
Read More

2955 snippets posted so far.