简体   繁体   English

GeoDjango 在 model 的模板表单上显示 map

[英]GeoDjango display map on template form from model

I am trying to build a sample website based on Django(GeoDjango) and OpenStreetMap.我正在尝试基于 Django(GeoDjango) 和 OpenStreetMap 构建一个示例网站。 So far I have this simple scenario:到目前为止,我有这个简单的场景:

Models.py模型.py

class Parks(models.Model):
park_name_en = models.CharField(max_length=256)    
description = models.TextField()
picture = models.ImageField()
geom = PolygonField()

@property
def picture_url(self):
    return self.picture.url

def __unicode__(self):
    return self.title

views.py视图.py

def park_insert(request):
form = ParkForm()
return render(request, 'addpark.html', {'form': form})

forms.py forms.py

class ParkForm(forms.ModelForm):
class Meta:
    model = Parks
    fields = ('park_name_en', 'description', 'picture',)
    geom = forms.PolygonField()

and last but not least the template addpark.html最后但并非最不重要的模板 addpark.html

    <html>
<head>
    {{ form.media }}
</head>

<body>
    <div id="map">
        <form enctype="multipart/form-data" method="post" action="">
            {% csrf_token %}
                {{ form.as_p }}
            <input type="submit" value="Submit"/>
        </form>
    </div>
</body></html>

When I open the template page all fields come up right, except the PolygonField() which comes up as a text.当我打开模板页面时,所有字段都会正确显示,除了以文本形式出现的 PolygonField()。 How can I display a map on the user form also (I got it working in the admin panel, but I want to create a form for inserting new parks)我怎样才能在用户表单上显示 map(我在管理面板中使用它,但我想创建一个用于插入新公园的表单)

You need to describe "forms " models.py.您需要描述“表单”models.py。

Models.py模型.py

from django.contrib.gis import forms


class Parks(models.Model):
    park_name_en = models.CharField(max_length=256)    
    description = models.TextField()
    picture = models.ImageField()
    geom = models.PointField(widget= forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500}) )

@property
def picture_url(self):
    return self.picture.url

def __unicode__(self):
    return self.title

views.py视图.py

def park_insert(request):
    form = ParkForm()
    return render(request, 'addpark.html', {'form': form})

forms.py表格.py

class ParkForm(forms.ModelForm):
class Meta:
    model = Parks
    fields = ('park_name_en', 'description', 'picture',)
    geom = forms.PolygonField()

Try it.尝试一下。 And looked this https://docs.djangoproject.com/en/2.2/ref/contrib/gis/forms-api/#widget-classes看着这个https://docs.djangoproject.com/en/2.2/ref/contrib/gis/forms-api/#widget-classes

I added widget in forms.py because adding this attribute in model causes exception.我在forms.py中添加了widget,因为在model中添加这个属性导致异常。

forms.py forms.py

from django.contrib.gis import forms
from .models import Memory
class AddMemoryForm(forms.Form, forms.ModelForm):
    class Meta:
        model = Memory
        fields = ['title', 'description', 'location']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-input'}),
            'description': forms.Textarea(attrs={'cols': 60, 'rows': 10}),
            'location': forms.OSMWidget
        }

views.py视图.py

class ShowMemory(LoginRequiredMixin, UpdateView):
    template_name = 'world/detail_memory.html'
    model = Memory
    form_class = UpdateMemoryForm

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['title'] = context['memory']
        return context

    def get_success_url(self):
        return reverse_lazy('memory')

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super().form_valid(form)

UpdateMemoryForm is the similar to AddMemoryForm UpdateMemoryForm 类似于 AddMemoryForm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM