Works like the [PREPEND_WWW](http://www.djangoproject.com/documentation/settings/#prepend-www) setting but, instead of adding, it removes the www.
Usage:
In the settings file add the UrlMiddleware to the middleware list and set REMOVE_WWW = True
This recipe raises an exception if there is an invalid variable in the template.
See [#6766](http://code.djangoproject.com/ticket/6766)
Note: I don't need this snippet any more, since I don't use the template language any more. For
my projects using only python is better (complex logic, simple layout).
This might be handy in countries where decimals are entered with a comma separating the decimal places from the integer part (for instance in Germany). It lets user enter and displays all decimals with a comma separator.
I ran into this problem and couldn't find a clean internationalized way of doing it... but newforms makes it so easy to roll your own. Hope it helps someone.
A TimeField that lets you parse a wide variety of freeform-text time descriptions. This doesn't inherit from TimeField because it doesn't use any of its functionality.
Includes unit tests demonstrating some examples of what the parser will and won't handle.
Have you ever felt the need to run multiple Django projects on the same memcached server? How about other cache backends? To scope the cache keys, you simply need to prefix. However, since a lot of Django's internals rely on `django.core.cache.cache`, you cannot easily replace it everywhere.
This will automatically upgrade the `django.core.cache.cache` object if `settings.CACHE_PREFIX` is set to a string and the Middleware contains `ScopeCacheMiddleware`.
A thread discussing the merging of this functionality into Django is available on [the dev mailing list](http://groups.google.com/group/django-developers/browse_thread/thread/d45edaafec56da2a).
However, (as of now) nowhere in the thread does anyone mention the reason why this sort of treatment is needed: Many of Django's internal caching helpers use `django.core.cache.cache`, and will then conflict if multiple sites run on the same cache stores.
Example Usage:
>>> from django.conf import settings
>>> from django.core.cache import cache
>>> from scoped_caching import prefix_cache_object
>>> settings.CACHE_PREFIX
'FOO_'
# Do this once a process (e.g. on import or Middleware)
>>> prefix_cache_object(settings.CACHE_PREFIX, cache)
>>> cache.set("pi", 3.14159)
>>> cache.get("pi")
3.14159
>>> cache.get("pi", use_global_namespace=True)
>>> cache.get("FOO_pi", use_global_namespace=True)
3.14159
>>> cache.set("FOO_e", 2.71828, use_global_namespace=True)
>>> cache.get("e")
2.71828
To Install: Simply add `ScopeCacheMiddleware` as a middleware and define `settings.CACHE_PREFIX` and enjoy!
Authentication backend for Simple Machines Forum user database.
Needs one setting in `settings.py`:
SMF_PREFIX = 'smf'
This is prefix of SMF tables. This shippet assumes that they are in the same database.
There is one more optional setting:
SMF_GROUP = 1
If set, will allow only users from group with given id. May be used to allow access to admin page only for moderators.
Uses jsmin or jspacker to compact javascript files (usage example in [this blog post](http://pedro.valelima.com/blog/2008/jan/17/deploying-compacted-javascript-django/))
Based on the discussion at Empty Thoughts (http://blog.michaeltrier.com/2007/8/6/age-in-years-calculation) I built a quick and dirty custom filter.
Save this as a file in your "templatetags" folder. I called mine "calculate_age.py" and then in your template "{% load calculate_age %}" then use it, "{{ object.birthdate|age }}"
The built-in escape filter only works with certain characters. It works great in environments where you can declare your charset (UTF-8). However, not everything can handle anything outside of the ASCII charset.
This replaces all non-ASCII characters with their encoded value as `®` for ®, for example.
Some template tags/filter for working with query strings in templates.
Examples:
{% load qstring %}
{% qstring %} # Prints current request's query string
{% qstring as current_qstring %} # Same but goes to context
{{ current_qstring|qstring_del:"key1" }} # Deletes all key1 values
{{ current_qstring|qstring_del:"key1&key2" }} # Deletes all key1 and key2values
{{ current_qstring|qstring_set:"key1=1&key2=2" }} # Deletes all old key1 and key2 values and adds the new values.
Template filter to format a start and end time in to a range. Uses Django's ["P" format](http://www.djangoproject.com/documentation/templates/#now) and assumes start and end time are on the same day or night before/morning after.
`{{ start_time|time_range:end_time }}`
Examples:
7-8 p.m.
8 p.m. - midnight
noon - 4 p.m.
9:45 a.m. - 5:15 p.m.
10:30 p.m. - 1:30 a.m.
This is a little helper for associating an owner to a newly created object, in this case making the assumption that the current user is always the owner. It removes the necessity of adding a custom save hook to your model.
get_current_user comes from this middleware trick to cache the current user:
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
With this middleware in place (add it to the MIDDLEWARE_CLASSES in your settings) you can pass a request to the model via a pre_save method on the model.
I'm not sure if it is an improvement over the [threadlocals method] (http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) but it may be an alternative that can be improved upon?
class MyModel(models.Model):
name = models.CharField(max_length=50)
created = models.DateTimeField()
created_by = models.ForeignKey(User, null=True, blank=True)
def pre_save(self, request):
if not self.id:
self.created_by = request.user
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.