Login

All snippets

Snippet List

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

Line & paragraph chopping

I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic") Use like so: {% load myExtraModule %} {{ blogpost.content|paragraphs:"2" }} The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.

  • chop
  • cut
  • line
  • paragraph
  • block
Read More

Serving null files

Sometimes you have to serve null files. This quick hacky generic view lets you do that. The best example of that is robots.txt, I want my robots.txt to always be empty, at least for now. The other example would be favicon.ico, but that's more often used to actually fill a purpose.

  • null
  • files
  • views
Read More

Updated version of #31

This is, I think, a slightly cleaner implentation of what [snippet 31](/snippets/31/) is trying to do; by starting off with a dictionary containing the things we want to look for, and using a list comprehension to kill anything which comes out of the form as `None`, we can avoid some of the intermediate data structures the other snippet was using, and hopefully get better performance. This is also quite a bit more maintainable, because supporting additional options now only requires adding a new key/value pair to `qdict`.

  • search
  • q-objects
Read More

Search in a model spanning relations

By popular demand an example of search in models that spans more realtions. Keep a list of Q, filter the None away, feed the rest to .filter() Credit goes to Carlo C8E Miron for the idea... cheers buddy! ;)

  • search
Read More

Contact Form

A simple way to get started using newforms. Implement a contact form. Mine saves the data in a table and sends a dedicated mailbox the feedback as well. I added support for TinyMCE as described in the django wiki. http://code.djangoproject.com/wiki/CustomWidgetsTinyMCE?format=txt Anyway use this as a starting point for writing your own form handling.

  • newforms
Read More

slugify js -> python

This code is derived from the slugify JS function used in django's admin interface. It will create django-compatible slugs for you. Sometimes I do batch imports and need my items to have slugs, I give this script the item's title and get a slug back. Simple

  • slugs
Read More

Mini issue tracker

Described more fully on [my blog](http://e-scribe.com/news/230), but the gist is: this model becomes a sort of mini-app in your admin, allowing you to record and track issues related to your other applications. Sorting still needs some work. UPDATED 2007-03-14: Fixed repeat in Admin.list_display (thanks, burly!); added Admin.list_filter; changed app list (why did I call that `PROJECTS`, anyway?) to omit "django.*" apps

  • admin
  • model
Read More
Author: pbx
  • 15
  • 31

assign fields dynamically in newforms

DynamicFieldSnippetForm demonstrates how to dynamically assign fields in newforms. 1. weight is a required static field 2. height is an optional dynamic field This example uses `request_height` as an optional keyword argument to declare whether the `height` field should be added to the form, but it's just there for demonstration purposes. If you decide to use a keyword argument in your code, be sure to pop it off (as demonstrated in the code) or you'll get an *unexpected keyword argument* error.

  • newforms
  • dynamic
  • fields
Read More

Getting dynamic model choices in newforms

This is an excerpt from the form code used on this site; the tricky bit here is making the `choices` for the `language` field get filled in dynamically from `Language.objects.all()` on each form instantiation, so that new languages can be picked up automatically. It also adds a blank choice at the beginning so that users can't accidentally ignore the field and incorrectly end up with whatever Language was first in the list. If you use this, always remember that you have to call the superclass `__init__` _before_ you set your dynamic choices, and that you need to accept `*args` and `**kwargs` so you can pass them to it. In theory, `ModelChoiceField` will solve this, but it still seems to be a bit buggy.

  • newforms
  • models
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

3109 snippets posted so far.