Login

Most bookmarked snippets

Snippet List

RestfulView

In the same vein as [snippet 436](http://www.djangosnippets.org/snippets/436/), this allows you to differentiate view logic by HTTP method such as GET, POST, PUT, DELETE. This is also very useful combined with the [HttpMethodsMiddleware snippet](http://www.djangosnippets.org/snippets/174/). I am not the author, but I have found it to be very helpful.

  • rest
  • http
  • urlconf
Read More

Templatetag for JS merging and compression

**Javascript merging and compression templatetag** One of the most important things for improving web performance is to reduce the number of HTTP requests. This is a templatetag that merges several javascript files (compressing its code) into only one javascript. It checks if this merged file is up to date, comparing modification time with the other javascripts to merge. Usage: {% load jsmerge %} {% jsmerge mergedfile js/file1.js js/file2.js js/file3.js %} The previous code will: 1. Search in `settings.MEDIA_ROOT` for all files passed by parameter. 2. Create a `/path/to/media_root/mergedfile.js` (if it doesn't exist or it's not up to date). This file is a merging plus compression of all javascripts. 3. Return this HTML fragment: `<script type="text/javascript" src="/media_url/mergedfile.js"></script>`

  • javascript
  • merging
  • compression
  • tunning
Read More

Timedelta template tag

This is tag similar to *timesince* and *timeuntil*, which work great until you starting giving timesince dates in the future or timeuntil dates in the past. Timedelta tag will humanize output correctly for both future and past dates using *now* as a default reference date (or a specified reference date as argument) now = Apr 27 2007 futuredate = May 5 2007 pastdate = Jan 5 2007 {{ futuredate|timedelta }} will output "in 1 week, 1 day" {{ pastdate|timedelta }} will output "3 months, 2 weeks ago"

  • template
  • date
  • time
  • humanize
Read More

Captcha Middleware

A middleware we are using to stop "spam" on Curse. It makes the user fill in a captcha box whenever they submit a form unless a cookie is set (which expires by default after 6 hours) See also [the template](http://www.djangosnippets.org/snippets/128/) Note: render_template is simply a shortcut function we have for doing render_to_response with a request context

  • captcha
  • anti-spams
Read More

Parsing and Highlighting &lt;code&gt; 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

Pygments Rendering Template Filter

Finds all ``<code></code>`` blocks in a text block and replaces it with pygments-highlighted html semantics. It tries to guess the format of the input, and it falls back to Python highlighting if it can't decide. This is useful for highlighting code snippets on a blog, for instance.

  • template
  • filter
  • pygments
  • highlighting
  • code
Read More

MultipleChoiceCommaField

MultipleChoiceCommaField - CheckboxSelectMultiple value sequence as a single string, comma-separated. Since I frequently want to store multiple-selected choice items as a single field in the model, I found it convenient to write a custom field class. The code uses the comma as a separator, but it could be easily generalized to use any delimiter.

  • newforms
  • checkbox
  • multiple
  • choice
  • selection
Read More

Amazon S3 browser-based upload form

A Django Form for creating a browser-based upload form that pushes files to Amazon S3. See http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434

  • forms
  • upload
  • s3
  • amazon
Read More

Dynamically add inlines

These functions use JQuery to dynamically add new entries for stacked or tabular inlines on a change form. To enable it, change the parent model to include this Javascript as well as JQuery. Here's an example: class MeetingAdmin(admin.ModelAdmin): inlines = [MeetingDonationInline, MeetingExtraInline] class Media: js = ["/media/jquery-1.3.2.min.js", "/media/dynamic_inlines.js"]

  • javascript
  • admin
  • jquery
  • inline
  • tabular
  • stacked
Read More

Firebug Lite Middleware

This middleware allows you to easily include the excellent debugging tool Firebug Lite in your projects. To install it, just add the middleware class to your list of installed middleware, pretty much anywhere in the list. If DEBUG is True, and your IP address is in the list of INTERNAL_IPS, Firebug Lite will load. It will, however, only load in browsers that are **not** Firefox, as I'm assuming that you have the **real** Firebug installed in Firefox. If you don't, go install it--what's wrong with you? Check out http://getfirebug.com/lite.html for more information.

  • middleware
  • debug
  • ie
  • debugging
  • firebug
  • msie
Read More
Author: jfw
  • 8
  • 15

Django model cron jobs

Inspired by [snippet 1126](http://www.djangosnippets.org/snippets/1126/), I thought I would post a module that stores crontab entries in a database, with a less powerful, but more end-user friendly API. ("twice a week starting on Sat 1 Dec 2007, 6:23am", instead of "23 6,18 * * *"). The crontab is synchronised when an entry is changed, and relevant environment variables, function name and arguments are provided. You might want to store this as an app called "scheduler" to match the imports. Enjoy!

  • cron-jobs
  • scheduled-events
Read More

keeping Model and Field History (everywhere)

Usage: class MyModel(ModelWithHistory): class History: model = True # save model changes into admin's LogEntry table fields = ('f1', 'f2') # save these fields history to AttributeLogEntry table f1 = CharField(max_length=100) f2 = IntegerField() for threadlocals, see http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Aware! Not thoroughly tested yet. May cause problems with loading fixtures.

  • model
  • history
Read More

server with debugging backdoor

this starts up two servers - HTTP serving the django application on port 8000 and a port 8001 server that will start an interactive interpreter with any incoming connections. this enables you to have an interpreter in the same process as your server. $ wget http://localhost:8000/someurl/ (...) $ nc localhost 8001 Python 2.5.2 (r252:60911, Jul 8 2008, 21:21:10) [GCC 4.3.1 20080626 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.db import connection >>> connection.queries [ ... ]

  • debugging
Read More

MarkdownTextField

A [common pattern in Django](http://code.djangoproject.com/wiki/UsingMarkup) is to create a TextField intended for Markdown text (i.e. description) and a companion non-editable TextField for storing the HTML version (i.e. description_html), so the Markdown converter need not be run for every page view. This snippet is a custom field which encapsulates this pattern in a single field which can automatically create and update its companion HTML field. Usage: class MyModel(models.Model): description = MarkdownTextField()

  • model
  • markdown
  • field
Read More

3110 snippets posted so far.