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.
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`.
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! ;)
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.
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
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
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.
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.
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.
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.
Convert plain text to html. For example:
text="""aabhttp://www.example.com http://www.example.com
http://www.example.com <<< [<aaaa></aaaa>]
"""
print plaintext2html(text)
It can convert url text to html href. And it also can convert space to . So if you paste python code, the indent will not lost at all. Default it'll convert \t to 4 spaces.
If you use javascript code with Django-template filter or other related things, it will be not sufficient to qoute string in javascript. This filter escape the string and quote it.
**Support good typography! Avoid widows! **
"Widows" are single words that end up on their own line, thanks to automatic line-breaks. This is an no-no in graphic design, and is especially unsightly in headers and other short bursts of text. This filter automatically replaces the space before the last word of the passed value with a non-breaking space, ensuring there is always at least two words on any given line. Usage is like so:
{{ blog.entry.headline|widont }}
It's a simple tag, but good typography is one of the hallmarks of a well-designed site that separates it from amateurish counterparts.
Note: The idea and name "widont" is copped directly from [Shaun Inman](http://shauninman.com), who wrote a [similar feature for WordPress](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin).