为什么要复制Django中的帖子数据,而不是直接使用它? [英] Why copy post data in Django instead of working with it directly?

查看:128
本文介绍了为什么要复制Django中的帖子数据,而不是直接使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 如果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 a copy() of them. That means you can’t change attributes of request.POST and request.GET directly.

这篇关于为什么要复制Django中的帖子数据,而不是直接使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆