Error django MultiValueDictKeyError

0

I have an error when updating it comes out, I have no problems when creating or deleting only when updating a record

Request Method: POST
Request URL:    http://localhost:8000/crud/edit/update/2
Django Version: 1.11.6
Exception Type: MultiValueDictKeyError
Exception Value:    
"'lat'"
Exception Location: C:\Python\Python36-32\lib\site-packages\django\utils\datastructures.py in __getitem__, line 85
Python Executable:  C:\Python\Python36-32\python.exe
Python Version: 3.6.3
Python Path:    
['C:\Users\Naoto\web',
 'C:\Python\Python36-32\python36.zip',
 'C:\Python\Python36-32\DLLs',
 'C:\Python\Python36-32\lib',
 'C:\Python\Python36-32',
 'C:\Python\Python36-32\lib\site-packages']
Server time:    Sat, 4 Nov 2017 22:05:02 +0000

For what it's worth, my views.py of my application

  from django.shortcuts import render, redirect
    from .models import Member

    # Create your views here.

    def index(request):
        members = Member.objects.all()
        context = {'members': members}
        return render(request, 'crud/index.html', context)

    def create(request):
        member = Member(nomb=request.POST['nomb'], address=request.POST['address'], 
        lat=request.POST['lat'], lng=request.POST['lng'], tipo=request.POST['tipo'])
        member.save()
        return redirect('/')

    def edit(request, id):
        members = Member.objects.get(id=id)
        context = {'members': members}
        return render(request, 'crud/edit.html', context)

    def update(request, id):
        member = Member.objects.get(id=id)
        member.nomb     = request.POST['nomb']
        member.address  = request.POST['address']
        member.lat      = request.POST['lat'],
        member.lng      = request.POST['lng'],
        member.tipo     = request.POST['tipo']
        member.save()
        return redirect('/crud/')

    def delete(request, id):
        member = Member.objects.get(id=id)
        member.delete()
        return redirect('/crud/')

Y por si acaso mi model.py de la app

from django.db import models

# Create your models here.dasd

class Member(models.Model):
    nomb    = models.CharField(max_length=60)
    address = models.CharField(max_length=80)
    lat     = models.DecimalField(max_digits=10, decimal_places=6)
    lng     = models.DecimalField(max_digits=10, decimal_places=6)
    tipo    = models.CharField(max_length=30)

    def __str__(self):
        return self.nomb + " " + self.address + " " + self.lat + " " + self.lng + " " + self.tipo

Try changing

 def update(request, id):
        member = Member.objects.get(id=id)
        member.nomb     = request.POST['nomb']
        member.address  = request.POST['address']
        member.lat      = request.POST['lat'],
        member.lng      = request.POST['lng'],
        member.tipo     = request.POST['tipo']
        member.save()
        return redirect('/crud/')

because of this but it does not update me in the Address and Type fields, it puts False and it does not update me even though there is another field odd

def update(request, id):
    member = Member.objects.get(id=id)
    member.nomb     = request.POST.get('nomb',False)
    member.address  = request.POST.get('address', False)
    member.lat      = request.POST.get('lat', False)
    member.lng      = request.POST.get('lng', False)
    member.tipo     = request.POST.get('tipo', False)
    member.save()
    return redirect('/crud/')

And taking advantage of this question I have my superuser but if I put localhost: 8000 / admin is my login page, but if I do not log in and put localhost directly: 8000 / crud enters my page how do I avoid this? in a simple way this is the first thing I do

    
asked by Naoto 04.11.2017 в 23:34
source

1 answer

0

Your problem is that you are sending a tuple, for python when you declare a variable with a comma at the end, python considers a tuple, for example:

a = x,

for python this statement is equivalent to:

a = (x, '')

and the field in your model does not accept tuples, you have to remove the comma at the end.

As for your second question, so that only registered users can access your views I advise you to use the decorator @login_required (). Example:

@login_required()
def update(request, id):
        member = Member.objects.get(id=id)
        member.nomb     = request.POST['nomb']
        member.address  = request.POST['address']
        member.lat      = request.POST['lat']
        member.lng      = request.POST['lng']
        member.tipo     = request.POST['tipo']
        member.save()
        return redirect('/crud/')

Black magic of python and Django, a beauty.

Greetings.

Adrian.

    
answered by 05.11.2017 в 01:32