如何在删除详细视图对象后进行重定向?

huangapple go评论66阅读模式
英文:

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 &gt; Threads &gt; 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(&#39;&#39;, views.index, name=&#39;index&#39;),

	path(&#39;forums/forum_create/&#39;,
		views.forum_create, name=&#39;forum_create&#39;),

	path(&#39;forums/forum_edit/&lt;int:id&gt;/&#39;,
		views.forum_edit, name=&#39;forum_edit&#39;),

	path(&#39;forums/forum_delete/&lt;int:id&gt;/&#39;,
		views.forum_delete, name=&#39;forum_delete&#39;),

	path(&#39;forums/&lt;int:id&gt;/&#39;,
		views.thread_list, name=&#39;thread_list&#39;),

	path(&#39;thread/&lt;int:year&gt;/&lt;int:month&gt;/&lt;int:day&gt;/&lt;slug:thread&gt;/&#39;,
		views.thread_detail, name=&#39;thread_detail&#39;),

	path(&#39;thread_comment/&lt;int:id&gt;/&#39;,
		views.thread_comment, name=&#39;thread_comment&#39;),

	path(&#39;thread_create/&lt;int:id&gt;/&#39;,
		views.thread_create, name=&#39;thread_create&#39;),

	path(&#39;thread_edit/&lt;int:id&gt;/&#39;,
		views.thread_edit, name=&#39;thread_edit&#39;),

	path(&#39;thread_delete/&lt;int:id&gt;/&#39;,
		views.thread_delete, name=&#39;thread_delete&#39;),

	path(&#39;search/&#39;, views.site_search, name=&#39;site_search&#39;),
]

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(&#39;page&#39;, 1)
	try:
		threads = paginator.page(page_number)
	except PageNotAnInteger:
		threads = paginator.page(1)
	except EmptyPage:
		threads = paginator.page(paginator.num_pages)

	return render(request, &#39;forum/thread_list.html&#39;,
								{&#39;forum&#39;: forum,
								&#39;threads&#39;: threads})


(...) # cut


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

	if request.method == &#39;POST&#39;:
		thread.delete()
		return redirect(&#39;forum:thread_list&#39;, id=forum) # this?

	return render(request, &#39;forum/thread_delete.html&#39;,
								{&#39;thread&#39;: thread,
								&#39;forum&#39;: forum})

thread_delete.html:

{% extends &#39;forum/base.html&#39; %}

{% block content %}
	&lt;h1 class=&quot;mt-5 pb-4&quot;&gt;Forums &gt; Threads &gt; Delete {{ thread.title }}&lt;/h1&gt;
	&lt;p&gt;Are you sure you want to delete thread &quot;{{ thread.title }}&quot;? All replies and
	other associated objects will be deleted along with it.&lt;/p&gt;

	{% load django_bootstrap5 %}
	&lt;form action=&quot;&quot; method=&#39;post&#39;&gt;
	  {% csrf_token %}
	  {% bootstrap_button button_type=&quot;submit&quot; content=&quot; Confirm and delete&quot; %}
	&lt;/form&gt;

{% 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(&#39;forum/thread_list.html&#39;, 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 == &#39;POST&#39;:
        thread.delete()
        return redirect(&#39;thread_list&#39;, id=forum_id)

    return render(request, &#39;forum/thread_delete.html&#39;, {&#39;thread&#39;: thread})

huangapple
  • 本文由 发表于 2023年3月4日 09:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定