Tengo un error al actualizar me sale, no tengo problemas al crear ni eliminar solo al actualizar un registro.
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
Por si sirve de algo, mi views.py de mi aplicación.
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
Intente cambiar
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/')
por esto pero no me actualiza en los campos Direccion y Tipo me pone False y no me actualiza aunque hay odificado otro campo
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/')
Y aprovechando esta pregunta yo tengo mi superusuario pero si pongo localhost:8000/admin es mi pagina para logueo, pero si no me logueo y pongo directamente localhost:8000/crud me entra a mi pagina como evito esto? de una manera sencilla esto es lo primero que hago