Login

All snippets

Snippet List

Smart append slash middleware

This middleware replaces the behavior of the APPEND_SLASH setting in the CommonMiddleware. Please set `APPEND_SLASH = False` and `SMART_APPEND_SLASH = True` if you are going to use this middleware. In your URL patterns, omit the trailing slash for URLs you want accessible without the slash. Include the slash for those URLs you wish to be automatically redirected from an URL with the slash missing. If a URL pattern exists both with and without a slash, they are treated as two distinct URLs and no redirection is done. Example urlpatterns = patterns('some_site.some_app.views', (r'^test/no_append$','test_no_append'), (r'^test/append/$','test_append'), (r'^test/distinct_url$', 'view_one'), (r'^test/distinct_url/$', 'view_two'), ) Behavior of URLs against the above patterns with SMART_APPEND_SLASH enabled: http://some_site/test/no_append → test_no_append() http://some_site/test/no_append/ → 404 http://some_site/test/append → http://some_site/test/append/ http://some_site/test/append/ → test_append() http://some_site/test/distinct_url → view_one() http://some_site/test/distinct_url/ → view_two() This module is also available [in our SVN repository](http://trac.ambitone.com/ambidjangolib/browser/trunk/middleware/common.py).

  • middleware
  • url
  • slash
Read More

Different approach to local settings.py

I like to keep all local settings files in my versioning repository. The way I differentiate between them is by querying the hostname of the local machine. I've got a host_settings folder with local settings files. Notice that the local settings file names go by a convention where '.' is replaced with underscores. Example: host_settings/local_machine_tld.py

  • settings
  • development
  • deployment
Read More

Logging and statistic middleware

Reviewing some statistic-middleware-classes I think you might need one wich is working correct and high-performant. Please comment it and have fun with it!

  • middleware
  • stats
  • logging
  • statistic
  • user-activities
Read More

Form row filter

I love newforms. But sometimes using ``{{ form }}`` within a template doesn't give you enough flexibility. The other option, manually defining the markup for each field, is tedious, boring and error-prone. This is an example of how you can use a template filter to get the best of both worlds. Use it like this to render an entire form: ``{% for field in form %}`` {{ field|form_row }} ``{% endfor %}`` Or use it on a per-field basis: ``<fieldset>`` {{ form.first_name|form_row }} {{ form.last_name|form_row }} ``</fieldset>``

  • newforms
  • forms
  • templates
  • filters
Read More

Another Memcache Status View

This was inspired by [this memcache status snippet](http://www.djangosnippets.org/snippets/54/) However, this version uses the quasi-internal cache._cache.get_status(), and it compiles a list of stats for each server that you specify in your CACHE_BACKEND setting.

  • memcache
Read More

django_template decorator

Easy way to caching template. Very simple usage: @django_template("my_site.html") def master_home(request): variables = { 'title' : "Hello World!" } return variables

  • template
  • render
  • decorator
Read More

XFN Form Field

**This is a newforms field for XFN relationships.** It normalizes input by removing excess whitespace, converting to lowercase and removing duplicates. This field also validates the relationship according to the [XFN profile](http://gmpg.org/xfn/11): `me` can only appear by itself and, at most, one value from each of the family, friendship and geographical categories is allowed. The `XFN_*` constants would probably be imported from somewhere else in practice, but are included here for simplicity.

  • newforms
  • fields
  • forms
  • form
  • field
  • xfn
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

SplitTimeField

The SplitTimeField and the corresponding widget SplitDateTimeWidget show two select boxes with one for hour from 0 to 23 and the other showing minutes 0,15,30 and 45 (can be customized very easily). Usage: ------- class TestForm(forms.Form): start_time = SplitTimeField(widget=SplitTimeWidget) end_time = SplitTimeField(widget=SplitTimeWidget)

  • forms
  • field
  • widget
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

Convert Unicode to ASCII

Unicode is great, but there are places where the conversion ends up with unintelligible characters. I first noticed this with curly quotes entered in forms on our site. `unicode_to_ascii` converts compound characters to close approximations in ASCII: such as umlaut-u to u, 1/2 (fraction glyph) to 1/2. You can add additional mappings in CHAR_REPLACEMENTS.

  • unicode
  • ascii
  • convert
Read More

Group results by a range of values in admin sidebar

Adds filtering by ranges of values in the admin filter sidebar. This allows rows in numerical fields to be grouped together (in this case, group by price): By store price All < 100 100 - 200 200 - 500 500 - 2000 >= 200 **To use:** 1. save the code as rangevaluesfilterspec.py in your app's directory 2. add `import rangevaluesfilterspec` to your models.py 3. add `myfield.list_filter_range = [value1, value2, ...]` to your filter field **Example:** from django.db import models import rangevaluesfilterspec class Product(models.Model): store_price = models.DecimalField(max_digits=10, decimal_places=2) store_price.list_filter_range = [100, 200, 500, 2000] class Admin: list_filter = ['store_price'] Note that two extra groups are added: less-than the lowest value, and greater-than-or-equal-to the highest value.

  • filter
  • admin
  • sidebar
Read More

3110 snippets posted so far.