2

I'm trying to create a simple form useful for put a point on a map.

models.py

from django.contrib.gis.db import models as geomodels
from django.db import models

class AddPoint(models.Model):
    name = models.CharField(max_length=100)
    geom = geomodels.PointField()

    def __str__(self):
        return self.name

forms.py

from django import forms
from django.contrib.gis import forms

from .models import AddPoint


class AddPointForm(forms.ModelForm):
    name = forms.CharField(
        max_length=100,
        widget=forms.TextInput(
            attrs={
                "type": "text",
                "class": "form-control form-control-lg",
                }
            ),
        )
    point = forms.PointField(
        widget=forms.OSMWidget(
            attrs={
                'map_width': 800,
                'map_height': 250,
                'default_lat': 0,
                'default_lon': 0,
                'default_zoom': 2,
                }
            ),
        )

    class Meta:
        model = AddPoint
        fields = [
            'name',
            'point',
            ]

views.py

def addPointOnMap(request):
    if request.method == "POST":
        geoform = AddPointForm(request.POST or None)
        if geoform.is_valid():
            new_point = geoform.save()
            return redirect('add_points_map')
    else:
        geoform = AddPointForm()
    context = {
        'geoform': geoform,
        }
    template = 'maps/editing/add_point.html'
    return render(request, template, context)

The template is a simple {{ geoform.as_p }}

When I click on "Add point", on the template, I see this message:

IntegrityError at /map/create/add-points/

null value in column "geom" violates not-null constraint DETAIL: Failing row contains (3, test, null).

From a week a try to solve this without results and I've seen that there aren't similar problem online. How I can solve this?

NB: If I use the admin panel I can add the points without problem

2

The error is this:

point = forms.PointField(...

This is the right way:

geom= forms.PointField(...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.