Login

A few jinja2 filters like django ones

Author:
brondsem
Posted:
March 13, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

Some of this is modified from http://www.djangosnippets.org/snippets/1062/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""" Convert linebreaks to <br/>s and escape each line.  Return value is marked 'safe' """
def linebreaksbr_escape(value):
    escaped_lines = []
    for line in value.split('\n'):
        escaped_lines.append(escape(line))
    escaped = "<br/>".join(escaped_lines)
    # mark as safe
    return Markup(escaped)

# reverse url pattern matching, and automatically urlencode parameters
# "|urlencode" filter is also available
def url(view_name, *args, **kwargs):
    escaped_args = tuple(_url_quote(arg) for arg in args)
    escaped_kwargs = dict((k, _url_quote(arg)) for k,v in kwargs)
    return url_noencode(view_name, *escaped_args, **kwargs)
def _url_quote(arg):
    if isinstance(arg,basestring):
        return urlquote(arg)
    else:
        return arg

# reverse url pattern matching
def url_noencode(view_name, *args, **kwargs):
    return reverse(view_name, args=args, kwargs=kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.