为什么要复制Django中的帖子数据,而不是直接使用它? [英] Why copy post data in Django instead of working with it directly?
问题描述
如果request.method ==POST:
post = request.POST.copy()
#do包含帖子数据
有没有理由复制帖子数据而不是直接使用它?
我认为这是因为 request.POST
本身是不可变的。如果你想要一个版本,你可以改变(可变性),你需要一个数据的副本来处理。
请参阅此链接(request.POST是一个QueryDict实例)。
类QueryDict
QueryDict
不可变,除非您创建一个copy()
。这意味着您不能直接更改request.POST
和request.GET
的属性。
Django code samples involving post data often shows code similar to this:
if request.method == "POST":
post = request.POST.copy()
#do stuff with post data
Is there a reason for copying the post data instead of working with it directly?
I think it is because request.POST
itself is defined immutable. If you want a version you can actually change (mutability), you need a copy of the data to work with.
See this link (request.POST is a QueryDict instance).
class QueryDict
QueryDict
instances are immutable, unless you create acopy()
of them. That means you can’t change attributes ofrequest.POST
andrequest.GET
directly.
这篇关于为什么要复制Django中的帖子数据,而不是直接使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!