[Answered ]-How to redirect after deleting a detail view object?

1πŸ‘

βœ…

The issue is with the line on your code "return redirect('forum/thread_list.html', id=forum)"
The redirect function should be given a URL name, not a file path. In this case, the URL name is β€˜thread_listβ€˜, not β€˜forum/thread_list.htmlβ€˜.

The redirect function expects a URL name so you need to pass the id of the forum as an argument to the URL. Try this code if this works. Instead of "forum = thread.forum" try "forum_id = thread.forum.id" and then pass this "forum_id" to redirect "id=forum_id"

@login_required
def thread_delete(request, id):
    thread = get_object_or_404(Thread, id=id)
    forum_id = thread.forum.id

    if request.method == 'POST':
        thread.delete()
        return redirect('thread_list', id=forum_id)

    return render(request, 'forum/thread_delete.html', {'thread': thread})

Leave a comment