Login

Convert XHTML-compatible shorttag to HTML-compatible tag.

Author:
clamothe
Posted:
April 22, 2008
Language:
Python
Version:
.96
Score:
0 (after 2 ratings)

This filter converts a XHTML-compatible shorttag <input ... /> to a HTML4-compatible tag <input ...>.

Example:

{% for field in form %} <dt>{{ field.label_tag }}</dt> <dd> {{ field.errors }} {{ field|remove_shorttag }} </dd> {% endfor %}

This will produce html4-compatible output, opposed to newform's normal XHTML output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()


shorttag_regex = re.compile('^<([^/>]*[^\s]+)\s*/>$')

@template.defaultfilters.stringfilter
def remove_shorttag(field):
	search = shorttag_regex.search(field)
	if search:
		try:
			return mark_safe(u'<%s>' % search.groups()[0])
		except:
			return mark_safe(field)
	else:
		return mark_safe(field)
remove_shorttag.is_safe = True

register.filter('remove_shorttag', remove_shorttag)

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.