Jun-19-2018, 11:19 AM
Hi Experts,
I have created an address form in Django2.0.6+Mysql+Python3.6 and it has location,address,latitude,longitude fields.
I want to auto filled the latitude and longitude fields once user entered the address.
For this purpose I have written following code-
In models.py-
in admin.py-
But when I open my address form I am getting following error in browser-
I have created an address form in Django2.0.6+Mysql+Python3.6 and it has location,address,latitude,longitude fields.
I want to auto filled the latitude and longitude fields once user entered the address.
For this purpose I have written following code-
In models.py-
1 2 3 4 5 6 7 8 9 10 11 12 |
from location_field.models.plain import PlainLocationField class Store(models.Model): location = PlainLocationField(based_fields = [ 'address' ], zoom = 7 , null = True ) @property def latitude( self ): latitiude, _ = self .location.split( ',' ) return latitiude @property def longitude( self ): _, longitude = self .location.split( ',' ) return longitude |
1 2 3 4 |
class StoreAdmin(admin.ModelAdmin): list_display = ( 'id' , 'chain' , 'name' , 'building' , 'description' , 'postal_code' , 'address' , 'phone' , 'latitude' , 'longitude' , 'opening_hours' ) readonly_fields = ( 'latitude' , 'longitude' ,) |
Error:Exception Type: ValueError
Exception Value:
not enough values to unpack (expected 2, got 1)
Exception Location: E:\mycms\models.py in latitude, line 230
The complete server log is as below-Error:Internal Server Error: /mycms/store/
Traceback (most recent call last):
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\db\models\options.py", line 566, in get_field
return self.fields_map[field_name]
KeyError: 'latitude'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\utils.py", line 276, in lookup_field
f = _get_non_gfk_field(opts, name)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\utils.py", line 307, in _get_non_gfk_field
field = opts.get_field(name)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\db\models\options.py", line 568, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Store has no field named 'latitude'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\handlers\base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = response.render()
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\template\library.py", line 214, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 326, in result_list
'results': list(results(cl))}
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 302, in results
yield ResultList(None, items_for_result(cl, res, None))
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 293, in __init__
super().__init__(*items)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 212, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "C:\Users\hitman\AppData\Local\Continuum\anaconda3\lib\site-packages\django\contrib\admin\utils.py", line 287, in lookup_field
attr = getattr(obj, name)
File "E:\mycms\models.py", line 214, in latitude
latitiude, _ = self.location.split(',')
ValueError: not enough values to unpack (expected 2, got 1)
Please someone help me to solve the issue!