How to redirect after deleting a detail view object?
Question:
Unable to redirect because the object no longer exists after deletion.
URLs:
urlpatterns = [
# home page
path('', views.index, name='index'),
path('forums/forum_create/',
views.forum_create, name='forum_create'),
path('forums/forum_edit/<int:id>/',
views.forum_edit, name='forum_edit'),
path('forums/forum_delete/<int:id>/',
views.forum_delete, name='forum_delete'),
path('forums/<int:id>/',
views.thread_list, name='thread_list'),
path('thread/<int:year>/<int:month>/<int:day>/<slug:thread>/',
views.thread_detail, name='thread_detail'),
path('thread_comment/<int:id>/',
views.thread_comment, name='thread_comment'),
path('thread_create/<int:id>/',
views.thread_create, name='thread_create'),
path('thread_edit/<int:id>/',
views.thread_edit, name='thread_edit'),
path('thread_delete/<int:id>/',
views.thread_delete, name='thread_delete'),
path('search/', views.site_search, name='site_search'),
]
view:
def thread_list(request, id):
forum = Forum.objects.get(id=id)
threads = forum.threads.all()
paginator = Paginator(threads, 20)
page_number = request.GET.get('page', 1)
try:
threads = paginator.page(page_number)
except PageNotAnInteger:
threads = paginator.page(1)
except EmptyPage:
threads = paginator.page(paginator.num_pages)
return render(request, 'forum/thread_list.html',
{'forum': forum,
'threads': threads})
(...) # cut
@login_required
def thread_delete(request, id):
thread = get_object_or_404(Thread, id=id)
forum = thread.forum
if request.method == 'POST':
thread.delete()
return redirect('forum:thread_list', id=forum) # this?
return render(request, 'forum/thread_delete.html',
{'thread': thread,
'forum': forum})
thread_delete.html:
{% extends 'forum/base.html' %}
{% block content %}
<h1 class="mt-5 pb-4">Forums > Threads > Delete {{ thread.title }}</h1>
<p>Are you sure you want to delete thread "{{ thread.title }}"? All replies and
other associated objects will be deleted along with it.</p>
{% load django_bootstrap5 %}
<form action="" method='post'>
{% csrf_token %}
{% bootstrap_button button_type="submit" content=" Confirm and delete" %}
</form>
{% endblock content %}
The forum = thread.forum
will not work because thread.
no longer exists after deletion? This is preventing me from redirecting the user back to the previous page?
Tried to delete and expected the user to return to the previous page.
Answers:
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})
Unable to redirect because the object no longer exists after deletion.
URLs:
urlpatterns = [
# home page
path('', views.index, name='index'),
path('forums/forum_create/',
views.forum_create, name='forum_create'),
path('forums/forum_edit/<int:id>/',
views.forum_edit, name='forum_edit'),
path('forums/forum_delete/<int:id>/',
views.forum_delete, name='forum_delete'),
path('forums/<int:id>/',
views.thread_list, name='thread_list'),
path('thread/<int:year>/<int:month>/<int:day>/<slug:thread>/',
views.thread_detail, name='thread_detail'),
path('thread_comment/<int:id>/',
views.thread_comment, name='thread_comment'),
path('thread_create/<int:id>/',
views.thread_create, name='thread_create'),
path('thread_edit/<int:id>/',
views.thread_edit, name='thread_edit'),
path('thread_delete/<int:id>/',
views.thread_delete, name='thread_delete'),
path('search/', views.site_search, name='site_search'),
]
view:
def thread_list(request, id):
forum = Forum.objects.get(id=id)
threads = forum.threads.all()
paginator = Paginator(threads, 20)
page_number = request.GET.get('page', 1)
try:
threads = paginator.page(page_number)
except PageNotAnInteger:
threads = paginator.page(1)
except EmptyPage:
threads = paginator.page(paginator.num_pages)
return render(request, 'forum/thread_list.html',
{'forum': forum,
'threads': threads})
(...) # cut
@login_required
def thread_delete(request, id):
thread = get_object_or_404(Thread, id=id)
forum = thread.forum
if request.method == 'POST':
thread.delete()
return redirect('forum:thread_list', id=forum) # this?
return render(request, 'forum/thread_delete.html',
{'thread': thread,
'forum': forum})
thread_delete.html:
{% extends 'forum/base.html' %}
{% block content %}
<h1 class="mt-5 pb-4">Forums > Threads > Delete {{ thread.title }}</h1>
<p>Are you sure you want to delete thread "{{ thread.title }}"? All replies and
other associated objects will be deleted along with it.</p>
{% load django_bootstrap5 %}
<form action="" method='post'>
{% csrf_token %}
{% bootstrap_button button_type="submit" content=" Confirm and delete" %}
</form>
{% endblock content %}
The forum = thread.forum
will not work because thread.
no longer exists after deletion? This is preventing me from redirecting the user back to the previous page?
Tried to delete and expected the user to return to the previous page.
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})