Login

Top-rated snippets

Snippet List

A tip for preserving GET arguments with pagination

This snippet shows a way to preserve GET arguments with pagination. Many people make mistakes to omit the query arguments besides page arguments for the pagination, and making sure correct may sphagettize your code.

  • template
  • get
  • view
  • pagination
  • arguments
Read More

RESTful class dispatch

Yet another implementation of class based RESTful dispatch. This particular implementation features: * You do not have to call __init__ from the derived classes. * Avoids __metaclass__ which (in our environment) led to unexpected method override behavior. * Method names match the google webapp API. * One new instance per request to reduce errors in multi-threaded code. Snippets of inspiration: * [436](http://www.djangosnippets.org/snippets/436/) * [437](http://www.djangosnippets.org/snippets/437/) * [1071](http://www.djangosnippets.org/snippets/1071/) * [1072](http://www.djangosnippets.org/snippets/1072/) * [1226](http://www.djangosnippets.org/snippets/1226/)

  • views
  • rest
  • http
Read More

Soft Timeout Middleware

This middleware implements a "soft timeout". This means the admin is sent an email whenever a page time exceeds a certian value. It is intended to run on production servers to inform the admin of any pages which are performing slowly.

  • middleware
  • timeout
Read More

Models with database views

This example shows, how to use database views with django models. NewestArticle models contains 100 newest Articles. Remember, that NewestArticle model is read-only. Tested with mysql.

  • sql
  • models
  • views
  • view
  • model
  • mysql
  • database
Read More

Simple Flatpage Navigation Items

Flatpages are great for simple html content. However, I wanted some way to associate a navigation menu (just a snippet of HTML) with one or more FlatPage objects. Additionally, I wanted to be able to edit these throught the Admin. This was my solution.

  • html
  • navigation
  • flatpage
  • nav
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

Captcha without Freetype or the Python Imaging Library (PIL)

If, like me, you've had trouble installing the [Python Imaging Library](http://www.pythonware.com/products/pil/) or [FreeType](http://freetype.sourceforge.org), you may have also had trouble getting a captcha to work. Here's my quick and dirty workaround — be warned, this is _very_ low level security, and shouldn't be used on high-profile sites. Originally published at <http://gregbrown.co.nz/code/django-captcha-without-freetype-or-pil/> Credit to the author of [django-simple-captcha](http://code.google.com/p/django-simple-captcha/), from whom I borrowed most of this code. ### Usage from django import forms from ABOVE-FILE import CaptchaField class CaptchaTestForm(forms.Form): myfield = forms.TextField() security_check = CaptchaField()

  • captcha
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

partial tag

This is another partial tag, taken from a previous partial tag. The previous one assumed template locations by hardcoding in "partials/%s", etc. I took all that out, and made it work. And I took out the not needed third parameter for passing in your own data So now you call like this: {% partial "partials/mypartial.html" %} It passes the template context var into the partial, so anything you do in the main template, will work in the partial Just make sure you've got all the right imports.

  • tag
  • python
  • partial
Read More

split_contents2 for template tags

Is an updated way of splitting contents for a token, it does the split, but fixes the list.. EX: From a tag call like this: {% partial "partials/template.html" %} usually you get: ['partial','"partials/template.html"'] notice the " double quotes fixes it with: ['partial','partials/template.html'] takes out the " quotes

  • template
  • tag
  • python
Read More

Signal to notify new saved comments

Signal to notify new saved comments. **Example:** from django.contrib.comment import models, signals signals.comment_was_posted.connect(new_comment_notifier, sender=models.Comment)

  • django
  • python
  • comments
  • signals
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

3110 snippets posted so far.