Error in Django Art matching query does not exist.

0

Good evening, everyone. This is my first post in the forum and I am also a bit lazy in django. I'm developing a project that involves a model called Art and in one of the views I need to do the data update. I made the whole part of the form and it arrives in the template, but when I click on the button to save the changes I get the error "Art matching query does not exist."

Below is the code for the view. If you need any more, I'm at your disposal. Thanks in advance.

def editarte(request):
id_arte = request.GET.get("id")
arte = Arte.objects.get(id = id_arte)
formEditArte = EditArteModelForm(request.POST or None, instance = arte)
if request.method == 'POST':
    if formEditArte.is_valid():
        arte.save()
        return redirect('/editarte')

formEditArte = EditArteModelForm()
context = {
    'formEditArte' : formEditArte,
    'arte': arte
}
return render(request, 'editarte.html', context)
    
asked by anonymous 17.12.2017 / 06:26

1 answer

0

It is possible that the record you are attempting to retrieve per id (id_arte) does not exist in the database. By the way then, you would want to first check if the record exists before you enter. If this is the case, put this code snippet in a try / except block to handle the exception:

try:
   arte = Arte.objects.get(id = id_arte)
   // Atualiza o objeto existente
except Arte.DoesNotExist:
   // Cadastre o novo objeto
    
17.12.2017 / 14:02