Answer the question
In order to leave comments, you need to log in
How to remove an object from a Django database?
Greetings. I'm making a blog in Django, I created a "delete" button on the article's detailed view page, but I can't delete the object from the database.
Url.py
....
url(r'^(?P<a_id>\d+)/del', del_post, name='del_view'),
....
def del_post(request, a_id):
article = Article.objects.get(id=a_id)
if request.user == article.author:
Article.objects.get(id=a_id).delete()
return redirect('/')
else:
return HttpResponse('Nonono!')
<a href= {% url 'del_view' a_id=Article.id %}> (Удалить) </a>
Answer the question
In order to leave comments, you need to log in
Solved the problem. It turned out that I had another model attached to the Article model, which I forgot about. Deleted it and everything worked.
Try like this:
def del_post(request, a_id):
article = Article.objects.get(id=a_id)
if request.user == article.author:
article.delete()
return redirect('/')
else:
return HttpResponse('Nonono!')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question