英文:
How to redirect after deleting a detail view object?
问题
无法重定向,因为对象在删除后不再存在。
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'),
]
视图:
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 %}
forum = thread.forum
不会起作用,因为在删除后 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.
答案1
得分: 1
代码部分已被保留,以下是翻译后的内容:
问题出在您的代码行上:“return redirect('forum/thread_list.html', id=forum)
”
重定向函数应该提供一个URL名称,而不是文件路径。在这种情况下,URL名称是'thread_list
',而不是'forum/thread_list.html
'。
重定向函数需要一个URL名称,因此您需要将论坛的id作为参数传递给URL。如果这段代码能够运行,请尝试这样做。不要使用“forum = thread.forum
”,而是使用“forum_id = thread.forum.id
”,然后将“forum_id”传递给重定向,“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})
英文:
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})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论