[Django]-Django custom form field incorrectly renders value

3πŸ‘

βœ…

I modified my widget render() method to check for the type of the value parameter passed and convert it into a LatLong object if needed as such:

def render(self, name, value, attrs=None):
    if not isinstance(value, LatLong):
        value = value.split(',')
        value = LatLong(lat=value[0], long=value[1], addr=value[2:])

    output = super(LatLongWidget, self).render(name, '%s,%s,%s' % (value.lat, value.long, value.addr), attrs)

    output += render_to_string('maps/coord.html',
                                {'GMAPS_API_KEY':GMAPS_API_KEY,
                                 'latlong': value,
                                 'id':attrs['id'],})

    return mark_safe(output)

That seems to have fixed my problem since the β€˜value’ parameter passed into render() is either a unicode string (when submitting the form) or a LatLong object when displaying the form.

πŸ‘€sm0ke21

Leave a comment