Delete Thing Example


#1

Hi Tracy,

I have been working away on an inventory project and I have a need to delete ‘things’ as well as create and update. I know that you have a delete uploads ability in your hellowebapp project, but this is a bit different. I have tried altering the delete uploads code to suit but I cannot get it to work. (I will also be using the upload functionality as well).
I have tried for days now and despite trawling through google, I cannot find a workable solution and I keep getting all sorts of errors from the dreaded ‘no reverse found’ (I hate this error) to ‘item matching query does not exist’. The latter I believe is that the record has been deleted, but I don’t know what to do in code after this.

@login_required
def delete_item(request, id):
    # grab the item
    item = Item.objects.get(id=id)

    # security check
    #if item.thing.user != request.user:
        #raise Http404

    # delete the image
    item2delete = item.delete()

    # refresh the edit page
    return render(request, 'items/edit_item.html', {'item': item, })
#    return redirect('items/items.html')   #, slug=item.slug)

Obviously, I am calling my ‘things’ as ‘items’ as in stock items.
In the above I also was going to use the return value of the item delete as this will be of some use.
The commented out return redirect does not work either but I kept it there so that I have something to work on.

Any assistance or pointers would be appreciated.

Kind regards

David


#2

Hey David, I wanted to apologize — apparently this forum software stopped emailing me notifications when posts were added, and I completely missed this. :(

Still need help?


#3

Hi Tracy, I would still like some help, if you wouldn’t mind. I also see that you have had this notification problem occur before on a couple of occasions.

David


#4

All from different problems, I assure you.

In the above I also was going to use the return value of the item delete as this will be of some use.

Yeah, AFAIK you don’t have anything that Django can access after deletion, so you shouldn’t expect to be able to access the object (through item or item.slug). Buuuut what you could do is assign the data that you want to a variable (item_slug = item.slug), delete the item, and then return the variable. Would that work?


#5

Hi Tracy,

Oh boy… I’d totally forgotten about this, but what you have suggested would work for me, thanks. Can’t believe I didn’t think of that myself.

Dave