Login

Most bookmarked snippets

Snippet List

Django app WSGI

If you want to have your wsgi as general as possible (for eg. different environments) without any hardcoded paths, this example might help you...

  • wsgi
Read More

Custom nose runner

This method is replacement for django test runner. It uses nose test runner, which will discover all tests in the application (not even in tests module of applications). For installing put in settings.py: `TEST_RUNNER = 'nose_runner.run_tests'` where *nose_runner* is module containing the code

  • test
  • nose
  • testrunner
Read More

EditingMiddleware (quickly open views and templates in your text editor!)

Heavily based on [Snippet 1033](http://www.djangosnippets.org/snippets/1033/) and [Snippet 766](http://www.djangosnippets.org/snippets/766/). This snippet tracks what view and templates are used to render HTML responses and inserts a small dialog in the top right corner of the output with links to all the files. If your text editor support opening files from a browser protocol you can click the links to open the files right up! For example TextMate supports the `txmt://` protocol. Really saves some time if you find yourself editing a lot of templates. ![txt](http://img.skitch.com/20090602-11df2tfg273p99i95nfx6a74qe.png) **Usage** 1. Save this snippet in a file called `middleware.py` on your Python Path. 2. Add `middleware.EditingMiddleware` to your `MIDDLEWARE_CLASSES`. 3. Browse to any HTML page on your site!

  • middleware
  • editing
  • texteditor
  • editor
Read More

Sometimes Tag

Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20. {% sometimes %} <img src='spy_behind_sniper.jpg'/> {% else %} <img src='sniper.jpg'/> {% endsometimes %} -- or -- {% sometimes .001 %} You win! {% else %} Sorry, not a winner. Play again! {% endsometimes %} -- or -- {% sometimes .5 %} This shows up half the time. {% endsometimes %}

  • tag
  • random
Read More

Easy Conditional Template Tags

This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition). All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • condition
  • else
  • endif
Read More

Cleanup dirty HTML from a WYSIWYG editor

My admin allows editing of some html fields using TinyMCE, so I end up with horrible code that contains lots of nested `<p>`, `<div>`, `<span>` tags, and style properties which destroy my layout and consistence. This tag based on lxml tries to kill as much unneeded tags as possible, and style properties. These properties can be customized by adapting the regex to your needs.

  • html
  • wysiwyg
  • tinymce
  • lxml
  • dirty
  • cleanup
Read More

Generic Permissions

A mixin to define permissions on models. This is more of an abstract model to subclass/customise than a plug-in solution. Explanations are [here](http://www.muhuk.com/2009/05/django-permission-system/).

  • models
  • model
  • permission
  • authorization
Read More

Content Moderator

This snippet is for [django-flag](http://code.google.com/p/django-flag/) Pinax app to make it generic moderator for any content model. You don't need to modify neither your model nor your views to moderate your flagged content objects.

  • generic
  • pinax
Read More

Naked CSS Day TemplateTag

This TemplateTag simply returns a True when it is Naked CSS Day allowing you to hide your CSS or display a custom message on that date. Allows allows for parameters should the date change (again).

  • templatetag
  • css
  • nakedcss
  • naked
Read More

Dynamic import from an installed app

This is an example how to dynamically import modules other than `models.py` from installed apps. It allows you to define the full module path just once in `INSTALLED_APPS` in the settings. Using this technique makes it possible to write extensible and reusable apps as mentioned in [Abstract Models and Dynamicly Assigned Foreign Keys](http://djangotricks.blogspot.com/2009/02/abstract-models-and-dynamicly-assigned.html). Probably, it would be great to have some helpers for doing this in django itself. For example: from django.db import models from django.utils import importlib def import_installed(path): """ Imports a module from an installed app >>> import_installed("myapp.forms") <module 'myproject.apps.myapp.forms'> """ app_name, module = path.split(".", 1) app = models.get_app(app_name) return importlib.import_module(app.__name__[:-6] + module) UPDATE: Reported as ticket [#10703](http://code.djangoproject.com/ticket/10703)

  • dynamic
  • import
Read More

Case Insensitive Authentication Backend

By enabling this backend: AUTHENTICATION_BACKENDS = ( 'path.to.my.backends.CaseInsensitiveModelBackend', ) Your users will now be able to log in with their username, no matter whether the letters are upper- or lower-case.

  • auth
  • case
  • case-insensitive
  • backend
  • insensitive
Read More

Pagination shortcut

This is a function wrapping the code from [example](http://docs.djangoproject.com/en/dev/topics/pagination/#using-paginator-in-a-view) from django docs. The required parameters are: `request` which is a `Request` object from a view, and `objects` - a list of objects to paginate. You may want to tune number of items per page by specifying `count` and the name of a GET parameter through `param_name` To use it in your view just wrap the paginated object into a function for example: def someview(request): articles = Article.objects.all() ... some other logics ... return render_to_response(template, {'articles': paginate(request, articles)}

  • pagination
Read More

3110 snippets posted so far.