Home > OS >  how I can insert and update (edit) latitude and long using (form.py) python Django
how I can insert and update (edit) latitude and long using (form.py) python Django

Time:04-12

I want to insert latitude and long using python Django. I use code but does not work when click on the button it shows null in DB.

models.py

class UserLocation(models.Model):
    map_id = models.AutoField(primary_key=True)
    map_u_address = models.CharField(max_length=250, null=True)
    latitude = models.DecimalField(max_digits=11, decimal_places=7, null=False, blank=True)
    longitude = models.DecimalField(max_digits=11, decimal_places=7, null=False, blank=True)

view.py

def save_location(request):
    if request.method == 'POST':
        form = request.POST

        latitude = form.get('latitude')
        longitude = form.get('longitude')
        user_id = request.session['user_id']


        insert_data = UserLocation.objects.create(                latitude=latitude,longitude=longitude,

                                                  )
        if insert_data:

            json_data = {'msg': "Insert data successfully", 'state_val': 1}
            return JsonResponse(json_data)
        else:
            json_data = {'msg': "Data not saved", 'state_val': 2}
            return JsonResponse(json_data)
    else:
        return render(request, 'map_1.html')

how I can do an update (edit) for latitude and long using (form.py) python Django?

CodePudding user response:

In forms.py

from django.forms import ModelForm
# Import your UserLocation model
from .models import UserLocation

# Create your forms here
class UserLocationForm(ModelForm):
    class Meta:
        model = UserLocation
        fields = ('latitude', 'longitude')

In views.py

from .forms import UserLocationForm

def save_location(request):
    form = UserLocationForm()
    context = {}
    context['form'] = form
    if request.method == 'POST':
        form = UserLocationForm(request.POST)
        user_id = request.session['user_id']
        if form.is_valid():
            insert_data = form.save()
            if insert_data:
                json_data = {'msg': "Insert data successfully", 'state_val': 1}
                return JsonResponse(json_data)
            else:
                json_data = {'msg': "Data not saved", 'state_val': 2}
                return JsonResponse(json_data)
        else:
            # Update form in context dictionary
            context['form'] = form
            # Re-render page with prefilled data
            return render(request, 'map_1.html', context)
    else:
        return render(request, 'map_1.html', context)

In template

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit">
</form>

For Updating location

In views.py

def update_location(request, location_id):
    try:
        # Get location from db for location_id
        location = UserLocation.objects.get(id=location_id)
    except UserLocation.DoesNotExist:
        return # redirect user back as the location id is invalid
    # Populate the form with data of requested instance
    form = UserLocationForm(instance=location)
    context = {}
    context['form'] = form
    if request.method == 'POST':
        form = UserLocationForm(request.POST)
        user_id = request.session['user_id']
        if form.is_valid():
            form.save()
            return # Redirect user to somewhere
        # When the posted data is invalid
        else:
            # Update form in context dictionary
            context['form'] = form
            # Re-render page with prefilled data
            return render(request, 'update_location.html', context)
    else:
        return render(request, 'update_location.html', context)

In update_location.html

{% block body %}
    <div >
        <form method="POST" action="">
            {% csrf_token %}
            {{ form }}
            <input type="submit" value="Upload" >
        </form>
    </div>
{% endblock body %}

In urls.py

path('update_location/<int:location_id>/', views.update_location, name="update-location"),
  • Related