我正在构建一个博客应用程序,当发布 comments 和回复 comments 时,页面重定向到帖子页面的顶部,我希望它重定向到发布的 comments \回复,或者滚动到它,如果你愿意的话.
例如,在发布时,我希望它达到http://127.0.0.1:8000/post/first#comment-12 要实现类似于以下内容的内容:
return HttpResponseRedirect(reverse(f'post_page#comment-{str(comment.id)}', kwargs={ 'slug': slug }))
Views.py
if request.POST:
comment_form = CommentForm(request.POST)
if comment_form.is_valid:
if request.POST.get('parent'):
parent_id = request.POST.get('parent')
parent = Comments.objects.get(id=parent_id)
if parent:
comment_reply = comment_form.save(commit=False)
comment_reply.parent = parent
comment_reply.post = post
comment_reply.save()
else:
comment = comment_form.save(commit=False)
post_id = request.POST.get('post_id')
post = Post.objects.get(id=post_id)
comment.post = post
comment.save()
return HttpResponseRedirect(reverse('post_page', kwargs={ 'slug': slug }))
Urls.py
urlpatterns = [
path('', views.index, name='index'),
path('post/<slug:slug>', views.post_page, name='post_page')
]