This template filter is meant to insert soft hyphens ([­ entities](http://www.cs.tut.fi/~jkorpela/shy.html)) in text whever it can. For this is relies on a **recent** checkout of the [PyHyphen](http://code.google.com/p/pyhyphen/) interface to the hyphen-2.3 C library, which is also used by Mozilla and OpenOffice.org.
It takes two optional parameters: the language to hyphenate in and the minimum word length to consider for hyphenation. If no language is given, the default language from the settings file is used. The second parameter defaults to 5 characters.
Usage example:
{% load hyphenation %}
{{ object.text|hyphenate:"nl-nl,6" }}
This filter links twitter user names prefixed with '@', hash tags, and URLs.
Usage example:
`{{var|twitterize}}`
Note: Do not use this in conjunction with the urlize filter. Twitterize does this for you.
Executive summary: url "include" on steroids--granular extra parms and validate names in passing
We maintain multiple Django applications, and we use the excellent built-in include mechanism to allow one urls.py to borrow from another:
http://docs.djangoproject.com/en/dev/topics/http/urls/
If you scroll down to the section entitled "Passing extra options to include," you will see this annoying limitation:
'''
Note that extra options will always be passed to every line in the included URLconf, regardless of whether the line's view actually accepts those options as valid. For this reason, this technique is only useful if you're certain that every view in the included URLconf accepts the extra options you're passing.
'''
My snippet overcomes this limitation, allowing you to extend individual urls without polluting the namespace. The function also has the nice side effect of validating that the parent hasn't changed names on you.
entity encoded string for a somewhat safer email-address.
this filter encodes strings to numeric entities, almost every standard-browsers decodes the entities and display them the right way.
needless to say that bots are smart, so this is not a 100% guaranteed spam prevention.
As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.
Example mixin for creating and removing thumbnails on a model. Also adds some methods for accessing an url for an image size. It might be a better idea to use a custom image field instead of this approach.
Idea for getting the image url for different sizes from http://code.google.com/p/django-photologue
Example usage
<pre>
>>> p.get_small_image_url()
u'http://127.0.0.1:8000/site_media/photos/small_someimage.jpg'
</pre>
Based on [svetlyak](http://www.djangosnippets.org/users/svetlyak/)'s [nofollow filter](http://www.djangosnippets.org/snippets/312/). This one processes only external URL's. Links with internal URL's will be returned unmodified.
There's one gotcha; I preferred to have false positives instead of false negatives. So, it will nofollow `href="some/relative/path/"` for example. If you must do relative paths do it like this:
<a href="./some/relative/path">link text</a>
I needed the ability to serialize and deserialize my database, which contains millions of objects. The existing XML serializer encountered spurious parse errors; the JSON serializer failed to handle UTF-8 even when it was asked to; and both the JSON and YAML serializers tried to keep all the representations in memory simultaneously.
This custom serializer is the only one that has done the job. It uses YAML's "stream of documents" model so that it can successfully serialize and deserialize large databases.
**Mobilize your Django site**
This is the code for a Django middleware class to allow you to easily mobilize your Django site. It makes use of [Wapple.net](http://wapple.net)'s Web services to provide device profiling and markup generation.
Using this middleware plugin, you can deliver your site to both mobile and web browsers using the same domain and exactly the same url structure and Python views. The WAPL markup language allows you to render sites to every single mobile device without worrying about the individual devices yourself.
**Requirements**
1. The [SUDS](https://fedorahosted.org/suds/) Python SOAP client.
2. A WAPL dev key. Sign up for one at [http://wapl.info](http://wapl.info)
3. The Django sessions framework must be enabled. See [http://docs.djangoproject.com/en/dev/topics/http/sessions/](http://docs.djangoproject.com/en/dev/topics/http/sessions/) for how to install.
**How To Use**
1. Save the code above as 'wapl_middleware.py' in the root of your project.
2. Replace 'YOUR-DEV-KEY-HERE' with your WAPL dev key.
3. In your project's 'settings'py', add the following to the bottom of your 'MIDDLEWARE_CLASSES' setting:
`'myapp.wapl_middleware.WAPLMiddleware',`
4. For each line in your 'TEMPLATE_DIRS' setting, create a new folder under that folder called 'wapl' e.g. for 'myapp/templates/', you would create the folder under 'myapp/templates/wapl/'.
5. For each template used in your application, write a WAPL version and save it in the corresponding 'wapl' directory. See [http://wapl.info/docs/chapter/Developing-with-WAPL/](the WAPL docs) for information about the WAPL markup language.
6. Django template inheritance and includes can be used as normal, so I recommend creating a 'base.html' like this one.
`<?xml version="1.0" encoding="UTF-8" ?>
<wapl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://wapl.wapple.net/wapl.xsd">
<head>
{% block wapl_head %}{% endblock %}
</head>
<layout>
{% block wapl_layout %}{% endblock %}
</layout>
</wapl>`
7. View your site from a mobile device, and you should see a nice mobile version.
**How It Works**
1. When a request is made, the middleware checks to see if a session variable is held telling us if the device is mobile or not.
2. If we don't already know, it calls the WAPL web services to check. It then stores this in the session for subsequent requests.
3. If the device is a mobile device, it appends 'wapl' to each line in your 'TEMPLATE_DIRS' setting. Your view code will work exactly the same as normal, and the 'wapl' templates will be used whenever a response is rendered.
4. When a response is about to be rendered, the middleware checks to see if the device is a mobile one.
5. If it is mobile, and the response about to be sent has a status code of 200 (OK), it sends the WAPL markup to the WAPL web service to generate the correct markup for that device.
6. Otherwise, it outputs the response unmodified.
**Tips**
1. Don't try to migrate your whole site to mobile - design for mobile and consider the user's goals on a handset.
2. If leaving sections out, don't just leave the wapl view out. Include one that says 'This page is not available on mobile'. This will make sure none of your external links are dead on the mobile version.
3. For full developer reference, information and schemas, see [http://wapl.info](WAPL.info).
If you have JSON objects with `datetime` attributes that you want to decode to python [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects, you can use `decode_datetime` as a [simplejson](http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html) object hook. `simplejson.loads(s, object_hook=decode_datetime)`.
If you want to do your own JSON serialization of [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects instead of using DjangoJSONEncoder, use `simplejson.dumps(o, default=encode_datetime)`. The `encode_datetime` method will convert the datetime object to UTC and output an ISO format string just like the [isoutc template filter](http://www.djangosnippets.org/snippets/1424/).
Hook the show_url_patterns view function in to your URLconf to get a page which simply lists all of the named URL patterns in your system - useful for if your template developers need a quick reference as to what patterns they can use in the {% url %} tag.
While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites.
Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can use those filters on datetime objects.
usage:
{{ object.content|wordbreak:"10" }}
This means if any word is 10 characters or longer, a `­` will be placed every 10 characters. This is to break long words which may break the appearance of a page.
The output of this is HTML safe as the content has been conditionally escaped.