Use this decorator on a function that returns a dict to get a JSON view, with error handling.
Features:
* response always includes a 'result' attribute ('ok' by default)
* catches all errors and mails the admins
* always returns JSON even on errors
The class LocationField renders a form field with map (from google maps) and a mark. It allows the user to drag the mark to point at some particular location, whose value (lat, lng) is saved in a hidden field.
It requires jquery and google maps.
Many models are tightly coupled to the default Django `User` model (`django.contrib.auth.models.User`).
Sometimes this user model just doesn't fit everyone's needs. By using `UserForeignKey` it is possible to make the `User` model configurable, encouraging loose coupling.
Additionally, this can help to prevent circular imports between `User` and another model.
Use it like a standard `ForeignKey`... it accepts all the same arguments.
If you want to use a `User` model other than the default, just add `USER_MODEL` to your settings file.... it uses dotted notation (`app_label.model_name`).
Example:
class BlogPost(models.Model):
user = UserForeignKey(related_name="blog_posts")
title = models.CharField(...)
content = models.TextField(...)
I was tired browsing via tag to find snippets I saw a while ago. So I created a custom search engine with Google.
To try it out go to [http://henning.cco-ev.de/django/djangosnippets.html](http://henning.cco-ev.de/django/djangosnippets.html)
Modified version of [Profiling Middleware](http://www.djangosnippets.org/snippets/186/)
Prints profile results for method, additionally groups results by files and by modules (for django uses top level modules as groups). Works for Windows.
Usage: append ?prof or &prof= to any URL pointing to django application after adding ProfileMiddleware to middlewares in yours settings.py.
NOTICE: ProfileMiddleware uses hotshot profiler which is not thread safe.
Requires the twitter module (easy_install python_twitter).
Your project settings file should define TWITTER_USERNAME.
Call the tag like:
`
{% get_twitter_status as tweet tweet_time tweet_url %}
<p><q cite="{{ tweet_url }}">{{ tweet }}</q> ({{ tweet_time }})</p>
`
**EDIT**: I've also included an alternative method as suggested in the comments. Called with:
`
{% get_twitter_status as tweet %}
<p><q cite="{{ tweet.url }}">{{ tweet.status }}</q> ({{ tweet.time }})</p>
`
[Original and further information available from here.](http://www.undefinedfire.com/articles/recursion-in-django-templates/)
**v1.1 Update (20/04/08):** Added the ability to recurse single elements as well as automatic skipping of empty elements.
Most of the tags are self explanatory, the only one that may cause confusion is the main `{% recurse %}` one. The format for this tag is `{% recurse [children] with [parent] as [child] %}` where “[children]” is the property that contains the children of the current element, “[parent]” is your starting element and “[child]” is the variable named used in the loop.
Example usage:
{% load recurse %}
... Headers and stuff ...
{% recurse category.category_set.all with categories as category %}
<ul>
{% loop %}
<li>
<h{{ level }}>{{ category.title }}</h{{ level }}>
{% child %}
</li>
{% endloop %}
</ul>
{% endrecurse %}
... The rest of the page ...
When given a model this utility method will export all data in that model to a CSV file. I use this method to place an "Export" button on a particular model's change list page in the Django administrator.
This is my personal tagging system that I have created. It is intended to be used for multiple applications. This tagging system also has a build in tag cloud that will generate based on the most frequently used tags that you have used or based on the number of clicks users have clicked on a particular tag.
Yet another SQL logger, this time with color and tresholds.
By default it will only print a summary line (total time, query count) unless one of the tresholds (total time, query count, individual query time) was exceeded.
You may use LOG_COLORSQL_VERBOSE = True to get the raw SQL for all requests regardless of any configured tresholds.
In either case the middleware will highlight problematic areas in yellow or red.
If you don't like the colors you can either set your tresholds to really high values, edit the code,
or use a different middleware. :-)
Tired of scrolling through hundreds of lines of code where the indentation is maddening?
Here's a middleware class that prettifys your html markup so it's nice and consistently indented. Intended only for debugging, and I add it to the middleware stack conditionally on TEMPLATE_DEBUG. Requires BeautifulSoup.
Have you had a rather huge database, say 50-100+ tables? Why not compress the tables you don't want to see and expand the ones that you do want to see? This template should do the trick. It uses cookies to remember which tabs you have open and which ones you have closed. This should work on .91 to current.
How to install - just CP code and save as index.html toss in your media folder under /path_to_media/template_folder/admin/index.html. next download mootools and toss this into /path_to_media/js_folder/mootools.js
(alternatively you could be lazy and cp mootools into the script tag of this template)
A quick and dirty fix but it sure saves time trying to find the tables you are looking for.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.