from django.forms import widgets from django.utils.safestring import mark_safe class LocationWidget(widgets.TextInput): def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs): self.based_field = based_field self.zoom = zoom self.width = width self.height = height super(LocationWidget, self).__init__(attrs) def render(self, name, value, attrs=None): if value is not None: lat, lng, zoom = value.split(',') value = '%s,%s,%d' % ( float(lat), float(lng), float(zoom) ) else: value = '' based_field = "#id_" + self.based_field.name attrs = attrs or {} attrs['data-location-widget'] = name attrs['data-based-field'] = based_field attrs['data-zoom'] = self.zoom attrs['data-map'] = '#map_' + name text_input = super(LocationWidget, self).render(name, value, attrs) map_div = u'''