Login

Snippets by joshua

Snippet List

SQL Log Middleware

This middleware will add a log of the SQL queries executed at the bottom of every page. You can (should) use BeautifulSoup to place this in a specific location. Note: If you serve non-html content, it would be wise to do a mimetype check.

  • sql
  • middleware
  • log
Read More

Regular Expression Replace Template Filter

This will perform a regular expression search/replace on a string in your template. `{% load replace %}` `{{ mystring|replace:"/l(u+)pin/m\1gen" }}` If: `mystring = 'lupin, luuuuuupin, and luuuuuuuuuuuuupin are lè pwn'` then it will return: `mugen, muuuuuugen, and muuuuuuuuuuuuugen are lè pwn` The argument is in the following format: [delim char]regexp search[delim char]regexp replace

  • regex
  • regular-expressions
  • templates
  • filters
Read More

Parsing and Highlighting <code> Blocks

This function takes a string (most likely from a template), searches it for `<code>[...]</code>`, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the `<code>[...]</code>` must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)

  • pygments
  • beautifulsoup
Read More

getattr template filter

Put inside `mysite/templatetags/getattr.py` Add `mysite` to your `INSTALLED_APPS` In your template: {% load getattr %} {{ myobject|getattr:"theattr,default value" }} Thanks to pterk for optimizations! \\o/

  • template
  • filter
  • get
Read More

Using Pygments with reST

UPDATED: This now supports an argument for the initial header level. This is a modified version of `django.contrib.markup` that allows you to highlight code via [pygments](http://pygments.pocoo.org/). The code block can be used as: `Here's a paragraph, and a code example: .. code:: language *insert code here* continue with your normal document.` Setup: Insert the snippet into `mysite/templatetags/rest.py`, then add `mysite` to your installed apps in `settings.py`. In your template, `{% load rest %}` and `{{ mycontent|rest }}`.

  • pygments
  • rest
  • restructured-text
  • template-filter
Read More

Using Textarea

A very common field in forms is the `<textarea>`, but `newforms` has no such field. Instead, you must use a dummy field (such as `newforms.CharField`) and use the `newforms.widgets.Textarea()` widget to render a textarea.

  • newforms
  • textarea
  • forms
Read More

joshua has posted 9 snippets.