Simple REST APIs
This is how I tend to roll simple REST APIs. It's by no means as full-featured as something like Piston, but it's simple, clean, and it works.
- rest
This is how I tend to roll simple REST APIs. It's by no means as full-featured as something like Piston, but it's simple, clean, and it works.
A quick and dirty helper for model field `choices`. It's not perfect, but this is what I use.
As a demo, I was asked to write a `render_to_file()` function to load a template and render it to a file. Turns out it's amazingly easy, and I think it's a neat trick to have in your bag of tools.
Meant mostly as a demo of how to do complex block tags, here's a switch/case implementation. It's deliberately simplistic: no default cases, no multiple values for the same case, etc. This is so that you can focus on the technique. Pay particular attention to how both switch and case pull out their child nodes and save them for later rendering. This is a very useful technique for these types of "structured" template tags.
A couple of utility `Node` subclasses that will automatically cache thier contents. Use `CachedNode` for template tags that output content into the template: class SomeNode(CachedNode): def get_cache_key(self, context): return "some-cache-key" def get_content(self, context): return expensive_operation() Use `CachedContextUpdatingNode` for tags that update the context: class AnotherNode(CachedContextUpdatingNode): # Only cache for 60 seconds cache_timeout = 60 def get_cache_key(self, context); return "some-other-cache-key" def get_content(self, context): return {"key" : expensive_operation()}
I'm posting this here mostly because I need a more permanent home for this than my lappy's hard drive. I hope it's interesting to other people, though. Anyway - this script is what I use to test Django against multiple versions of Python and multiple databases. To actually run this you'll of course need Python 2.3, 2.4, and 2.5 installed along with MySQL, PostgreSQL, and sqlite3 -- and the associated database wrappers for all 3 Pythons. Yes, for the record, I've got all those things installed on my laptop. If you can somehow make that work, though, running this script will print out a nice little summary of what's failing against which versions of Python and which database. Run with `-v` to see the actual failures.
People -- and by "people" I mean Jeff Croft -- often ask about how to split a list into multiple lists (usually for presenting as columns in a template). These template tags provide two different ways of splitting lists -- on "vertically", and the other "horizontally".
jacobian has posted 7 snippets.