Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Belgian Biodiversity Platform
astapor
Commits
a4756fd8
Commit
a4756fd8
authored
Jun 14, 2017
by
Nicolas Noé
Browse files
Admin: Custom lat/lon widget instead of OL map.
parent
98ffa2f0
Changes
3
Hide whitespace changes
Inline
Side-by-side
website/specimens/admin.py
View file @
a4756fd8
from
django.contrib
import
admin
from
specimens.models
import
Specimen
from
django
import
forms
from
.models
import
Specimen
from
.widgets
import
LatLongWidget
# Custom form to provide lat/lon widget instead of OL map.
class
MyAdminForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Specimen
fields
=
"__all__"
widgets
=
{
'location'
:
LatLongWidget
}
class
SpecimenAdmin
(
admin
.
ModelAdmin
):
pass
form
=
MyAdminForm
admin
.
site
.
register
(
Specimen
,
SpecimenAdmin
)
website/specimens/templates/django/forms/widgets/latlong.html
0 → 100644
View file @
a4756fd8
<p>
{{ lat_label }} {% with widget=widget.subwidgets.1 %}{% include widget.template_name %}{% endwith %}
{{ long_label }} {% with widget=widget.subwidgets.0 %}{% include widget.template_name %}{% endwith %}
</p>
\ No newline at end of file
website/specimens/widgets.py
0 → 100644
View file @
a4756fd8
from
django.forms
import
widgets
from
django.contrib.gis.geos
import
Point
class
LatLongWidget
(
widgets
.
MultiWidget
):
"""
A Widget that splits Point input into latitude/longitude text inputs.
"""
template_name
=
'django/forms/widgets/latlong.html'
def
__init__
(
self
,
attrs
=
None
):
_widgets
=
(
widgets
.
TextInput
(
attrs
=
attrs
),
widgets
.
TextInput
(
attrs
=
attrs
),
)
super
(
LatLongWidget
,
self
).
__init__
(
_widgets
,
attrs
)
def
get_context
(
self
,
name
,
value
,
attrs
):
context
=
super
(
LatLongWidget
,
self
).
get_context
(
name
,
value
,
attrs
)
context
[
'lat_label'
]
=
"Lat:"
context
[
'long_label'
]
=
"Lon:"
return
context
def
decompress
(
self
,
value
):
if
value
:
return
tuple
(
value
.
coords
)
return
(
None
,
None
)
def
value_from_datadict
(
self
,
data
,
files
,
name
):
mylat
=
data
[
name
+
'_0'
]
mylong
=
data
[
name
+
'_1'
]
try
:
point
=
Point
(
float
(
mylat
),
float
(
mylong
))
except
ValueError
:
return
''
return
point
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment