Django error Art matching query does not exist.

Asked

Viewed 3,010 times

0

Good evening, guys. This is my first post on the forum and I’m also a little layman on Jango. I’m developing a project that involves a model called Art and in one of the views I need to update data. I did the whole part of the form and it arrives in the template, but when I click on the button to save the changes I am presented with the error Arte matching query does not exist.

Below follows the code of the view. If you need any more, I am available. 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)
  • Cleverton, also displays the HTML of the form and the urls.py so I can take a look at how it’s being accessed

1 answer

1

It is possible that the record you are trying to recover by id (id_arte) does not exist in the database. By the way then, you would like to check first if the record exists before you enter. If this is the case, put this code snippet into a block Try/except to treat the exception:

try:
   arte = Arte.objects.get(id = id_arte)
   // Atualiza o objeto existente
except Arte.DoesNotExist:
   // Cadastre o novo objeto
  • In fact it already exists. I just want to edit this existing record. In the art template I click on a button below the art that redirects to this editart view. It opens normally and I can fill in the form, but when I click save the error happens.

  • Checks the generated html if the id of the record to be edited is set to an Hidden field. It may be that no value is being passed in this "id" parameter that you expect in your . py script.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.