GeoDjango OSMWidget坐标更改 - python

我想知道是否有办法知道OSMWidget坐标何时已更改,我假装将此更改反映在经度和纬度字段上。我有以下表格:

from django.contrib.gis import forms
from .models import Branch


class BranchCreateForm(forms.Form):
    name = forms.CharField(label ="Name", max_length=120)
    image_facade = forms.ImageField( label="Image Facade")
    longitude = forms.DecimalField(label = "Latitude", max_digits=9, decimal_places=6)
    latitude = forms.DecimalField(label = "Longitude", max_digits=9, decimal_places=6)
    location = forms.PointField(
        widget=forms.OSMWidget(
            attrs={'map_width': 600,
                   'map_height': 400,
                   'template_name': 'gis/openlayers-osm.html',
                   'default_lat': 42.1710962,
                   'default_lon': 18.8062112,
                   'default_zoom': 6}))

python参考方案

PointField包含longitude object that it creates的latitudePoint坐标。因此,您无需将它们另存为DecimalField

每当通过小部件进行更改并保存表单时,Point都会相应地更新。

假设您有一个Branch实例,则可以按以下方式访问坐标:

br = Branch.objects.get(pk=1)
longitude = br.location.x
latitude = br.location.y

声明后编辑由OP提供:

您无需在表单类中添加字段。
您需要访问模板中的特定表单字段:

{{ form.location.x|default_if_none:"" }}
{{ form.location.y|default_if_none:"" }}

资料来源:Display value of a django form field in a template?

处理后如何删除照片? - python

我的Django模型中有两个字段: class Staff(models.Model): photo = models.FileField(blank=True, null = True) encodings = JSONField() 我从表单获取照片,然后使用该照片获取编码。处理后如何删除照片?我试过了self.photo = None or self.…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

如何在Django中解决模块名称冲突? - python

创建Django应用程序时出错:python端发生错误。退出代码:1,err:CommandError:'untitled1'与现有Python模块的名称冲突,因此不能用作项目名称。请尝试使用其他名称。 python大神给出的解决方案 您正在使用哪个版本的python?升级您的django版本或降级您的python版本,这应该可以解决问题。您可以在cl中执行…

Forms.py文件应该放在哪里? - python

我现在开始写我的第一个Django项目,我需要为应用创建我的forms.py文件。我见过一些教程将文件存储在项目的主文件夹下,而另一些则存储在app目录中。如果我想制作仅适用于一个应用程序的表单,哪种布局最适合我?是否可以制作多个文件来保存表单代码?谢谢! python大神给出的解决方案 这是标准布局:├── apps/ | ├── [app]/ | | ├…

如何在Django中获取视图函数的URL路径 - python

举个例子:view.pydef view1( request ): return HttpResponse( "just a test..." ) urls.pyurlpatterns = patterns('', url( r'^view1$', 'app1.view.view1…