这是为我在 Django 中的帖子应用程序编写的视图。问题是在填写更新表单并成功提交后。但这会给用户带来困惑,因为存在相同的 HTML 页面,我如何才能重定向到更新后的对象?
def post_update(request,id=None):
instance=get_object_or_404(Post,id=id)
if instance.created_user != request.user.username :
messages.success(request, "Post owned by another user, You are having read permission only")
return render(request,"my_blog/denied.html",{})
else :
form=PostForm(request.POST or None,request.FILES or None,instance=instance)
if form.is_valid():
instance=form.save(commit=False)
instance.save()
context={ "form":form,
"instance":instance }
return render(request,"my_blog/post_create.html",context)
原文由 Self 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 http 快捷方式中的
redirect
。这是官方文档的 链接。