Login

Top-rated snippets

Snippet List

Caching tag with singnal-based invalidation

Example usage: @cached('/data/something_hard') def get_something_complex(): .... return result dispatcher.connect(get_something_complex.invalidate, django.db.models.signals.post_save, Model)

  • cache
  • decorator
  • invalidation
Read More

JSON view decorator

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

  • view
  • json
  • decorator
  • exception
Read More

widget to capture a geographic Point

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.

  • newforms
  • forms
  • gis
  • google
  • field
  • maps
  • widget
Read More

UserForeignKey

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(...)

  • foreignkey
  • user
  • auth
Read More

Search djangosnippets.org

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)

  • snippets
  • search
  • google
  • snippet
  • djangosnippets
  • cse
Read More

Extended Profiling Middleware

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.

  • middleware
  • profile
  • hotshot
Read More

Twitter status tag

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> `

  • template
  • twitter
Read More

Recurse template tag for Django

[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 ...

  • template
  • templatetag
  • recursion
Read More

Tagging System

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.

  • tag
  • django
  • python
  • tags
  • tagging
  • tagger
Read More

Color SQL logging middleware

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. :-)

  • sql
  • middleware
  • log
Read More
Author: moe
  • 4
  • 13

HTML Prettifier

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.

  • html
  • pretty
  • prettify
  • ugly
  • indented
Read More

Foldable Admin Interface

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.

  • django
  • admin
  • templates
  • html
Read More

3110 snippets posted so far.