Login

Most bookmarked snippets

Snippet List

DebugMiddleware footer with links to quick open file/line# in TextMate on local machine

Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed, the view function that was used and all templates that were loaded. Each template path is a clickable URL of the type txmt://open/?url=file://FILENAME&line=LINENO as is the view function. This lets you open the file very quickly in TextMate which speeds up development considerably and relieves the mind of much fetching-lookup tedium. This version works on SVN 12002 (1.2 alpha) and earlier. The previous version was: http://www.djangosnippets.org/snippets/1033/ which got caught but some edge case in certain modules. It is based on the original by Simon http://www.djangosnippets.org/snippets/766/ To use save this as 'debug_middleware.py' somewhere on your PYTHONPATH and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting. - Felix (the Third) hey wouldn't it be great if you could retrieve your password on djangosnippets ? somebody should volunteer to upgrade it

  • middleware
  • debug
  • textmate
Read More

Override QuerySet.delete() (one way of preventing cascading deletes)

We needed to override the default QuerySet delete function to deal with a client problem that we were facing Yes This is monkey-patching, and probably bad practice but if anyone needs to conditionally override the cascading delete that django does at the application level from a queryset, this is how to do it

  • queryset
  • delete
  • monkey-patch
Read More

Improved Multiple Model Files

Most other methods I've seen for splitting an app's models across multiple files involve adding a couple lines to every model. This method factors out those duplicate lines into one place.

  • model-package
  • multiple-model-files
Read More

Prevent form tampering of hidden fields

Sometimes, we need to pass hidden fields with an initial value in forms but cannot trust the returned values because it could have been tampered. So here is a form that adds an additional 'hidden' field (called 'form_hash') that hashes all the initial value of hidden fields and checks for tampering. Pretty straightforward to use.

  • forms
  • hidden
  • tampering
Read More

Request time logging middleware

Middleware class logging request time to stderr. This class can be used to measure time of request processing within Django. It can be also used to log time spent in middleware and in view itself, by putting middleware multiple times in INSTALLED_MIDDLEWARE. Static method `log_message' may be used independently of the middleware itself, outside of it, and even when middleware is not listed in INSTALLED_MIDDLEWARE.

  • middleware
  • debug
  • logging
Read More

Currency formatting filter

See the function **docstring** for usage. This template filter has a couple of drawbacks: * Uses **locale.setlocale** which, according to the [Python docs](http://docs.python.org/library/locale.html#locale.setlocale), is not thread safe. I don't know how this may affect Django applications. * Requires Python 2.5+. Updated 2011-03-16.

  • template
  • filter
  • currency
  • formatting
Read More

Front end admin toolbar

This snippet will monkeypatch `django.db.models.Model` to include 7 new methods: * `get_verbose_name` Because you can't access model._meta from templates * `get_verbose_name_plural` * `get_admin_change_url` * `get_admin_delete_url` * `get_admin_history_url` * `get_admin_changelist_url` * `get_admin_add_url` This snippet also gives you the template code to paste to your `base.html` so every front end model instance view of your site will show an admin toolbar for logged in users that have admin access.

  • admin
  • toolbar
Read More

Showell markup--DRY up your templates

The code shown implements a preprocessor for Django templates to support indentation-based syntax. The pre-markup language is called Showell Markup. It allows you to remove lots of close tags and random punctuation. It also has a syntax for cleaning up individual lines of HTML with a pipe syntax for clearly separating content from markup. You can read the docstrings to glean the interface. Here are examples: >> table >> tr >> td Right >> td Center >> td Left >> div class="spinnable" >> ul >> li id="item1" One >> li id="item2" Two %% extends 'base.html' %% load smartif %% block body %% for book in books {{ book }} %% if condition Display this %% elif condition Display that %% else %% include 'other.html' >> tr class="header_row" Original Author | th class="first_column" Commenters | th Title | th Action | th Last words | th By | th When | th >> ol class="boring_list" One | li Two | li Three | li {{ element4|join:',' }} | li Hello World! | b | span class="greeting" ; br Goodbye World! | i | span class="parting_words"; br ; hr >> p class="praise" Indentation-based syntax is so Pythonic! Archive LINK collection.archive referral.pk Home LINK home.home Friends LINK friendship.friends Create a card LINK referrals.create Recent changes LINK activity.recent_changes >> FORM referrals.create {{ form.as_p }} {{ referral.id } | HIDDEN referral

  • templates
  • dry
  • preprocessor
Read More

reorder apps in admin index

A quick & dirty way of sorting the apps on the admin index page however you want. It requires you to override the default admin/index.html template. The instructions are in the docstring, they assume you insert the code above in a templatetag file called admin_app_order.py

  • admin
  • reorder
Read More

safe(r) monkeypatching scheme for django testing

In test code, it is sometimes useful to monkeypatch a Django method to have stubbed out behavior, so that you can simplify data setup. Even with decent data setup you might want to avoid execution of Django code that is not the target of your test. The code snippet shown here illustrates a technique to limit the scope of your monkeypatches. It uses the Python "with" statement, which was introduced in 2.5. [with statement](http://effbot.org/zone/python-with-statement.htm) The key aspect of the "with" machinery is that you can set up an __exit__ method that gets called even if the code inside the "with" raises an exception. This guarantees that your monkeypatch gets un-monkeyed before any other code gets called. I don't recommend monkeypatches in production, but if you HAVE to resort to a monkeypatch, I definitely advise using "with" to limit their scope. The examples on the left illustrate how to suppress versions of reverse() and timesince()--look at the import statements to see which ones I am talking about. Obviously, monkeypatching is not for the faint of the heart, as you need to be able to find the code to monkeypatch in Django source, and you need to be sure there aren't decorators at play.

  • testing
  • monkeypatch
Read More

django paginator

This a basic pagination example. It shows new 5 pnews items. We added a code to our template so we can view previous or next pages. It also show us how many pages we have.

  • django
  • paginator
Read More

3110 snippets posted so far.