jsonify: Django custom template filter

This template filter converts the filtered variable to json. I added comment to that snippet to:

1. Pass a kwarg cls=ModelJSONEncoder to simplejson.dumps to get handling of django.db.models.Model objects (get ModelJSONEncoder here). And as sk1p pointed out, you’ll also get handling of datetime objects and decimals for free.

2. Put jsonify.is_safe=True after register.filter so you don’t need to do {% autoescape off %} or |safe every time you use jsonify.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s