使用Django重定向和HttpResponseRedirect有什么区别?

2024-03-28 12:24:46 发布

您现在位置:Python中文网/ 问答频道 /正文

一般用哪种比较好?

写作有什么好处吗

return redirect(my_url)

结束:

return HttpResponseRedirect(my_url)

还是直接化名?有什么区别吗?哪一个更像是Python?


Tags: httpsdevcomrefhttpurldocsreturn
2条回答

两者之间有区别:

HttpResponseRedirect的情况下,第一个参数只能是url

最终返回的HttpResponseRedirect可以接受modelviewurl作为“to”参数。因此,它在“重定向”到的内容上更灵活一些。

我也喜欢redirect是如何变短的。所以我用redirect代替HttpResponseRedirect

不过,两者都可以使用。

从文件中-

redirect(to[, permanent=False], *args, **kwargs) Returns an HttpResponseRedirect to the appropriate URL for the arguments passed.

从定义上看是一样的。这就是捷径的作用。两者是同一的。

快捷方式通常写在实际API之上一级。因此redirect用arg permanent=False封装了HttpResponseRedirectHttpResponsePermanentRedirect

redirect上使用HttpResponseRedirect没有大的缺点。希望这能解决问题。

相关问题 更多 >