# Sample model # #class Sitio(models.Model): # nombre = models.CharField(max_length=255) # location = models.CharField(max_length=255) # # class Admin: # list_display=('nombre', 'location',) # # # Sample view #class SitioForm(ModelForm): # location = LocationField() # class Meta: # model = Sitio # #def add_sitio(request): # # form = SitioForm({'name':'Nombre','location':u"40.42, -3.7"}) # Default initial values # if request.POST: # form = SitioForm(request.POST) # if form.is_valid(): # obj = form.save() # # return render_to_response("test/test.html", {'form':form}) # Sample template # #
# # # # # # from django import newforms as forms DEFAULT_WIDTH = 500 DEFAULT_HEIGHT = 300 class LocationWidget(forms.widgets.Widget): def __init__(self, *args, **kw): self.map_width = kw.get("map_width", DEFAULT_WIDTH) self.map_height = kw.get("map_height", DEFAULT_HEIGHT) super(LocationWidget, self).__init__(*args, **kw) self.inner_widget = forms.widgets.HiddenInput() def render(self, name, value, *args, **kwargs): if isinstance(value, unicode): a, b = value.split(',') else: a, b = value lat, lng = float(a), float(b) js = ''' ''' % dict(name=name, lat=lat, lng=lng) html = self.inner_widget.render("%s" % name, "%f,%f" % (lat,lng), dict(id='id_%s' % name)) html += "" % (name, self.map_width, self.map_height) return (js+html) class LocationField(forms.Field): widget = LocationWidget def clean(self, value): if isinstance(value, unicode): a, b = value.split(',') else: a, b = value lat, lng = float(a), float(b) return "%f,%f" % (lat, lng)