E
E
expp2020-06-05 18:35:35
Django
expp, 2020-06-05 18:35:35

How to make a warning page before deleting a post?

Good afternoon. Let's agree that we have a website where you can create topics and make notes on these topics. There is a function to delete an entry (indents are observed):

spoiler
def remove_entry(request, entry_id):
  """Удаляет конкретную запись"""
  entry = Entry.objects.get(id=entry_id)
  topic = entry.topic
  if topic.owner != request.user:
    raise Http404
  entry.delete()
  return redirect('learning_logs:topic', topic_id=topic.id)


How can I implement a record deletion confirmation page? In other words, when you click the Delete button (it has already been created), there would be a transition to a page that would display a question and two buttons: "Yes" or "No". When you click on the "Yes" button, this record would be deleted (that is, the function would just be executed), and when you click on the "No" button, you would go to the page with the records, which logically has not changed in any way, because we refused deletion (that is, the function should not be executed). Thank you)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-11
@expp

Well, you need to do the following, on the "Delete Entry" button you make a link to the delete page. On the delete page, you add a form. And the form calls a POST request to the same page. In the same place you can add a link "Return to the record".
The logic is this, if there is a GET request on this shame, then you display a deletion confirmation, that is, a form. And if the request is POST, then you are already performing the deletion itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question