[django]更改 request.GET 字典的键值时出现 "AttributeError: This QueryDict instance is immutable" 错误

当修改 request.GET/request.POST 时,会出现: "AttributeError: This QueryDict instance is immutable":

def foobar(request):
    #...
    request.GET['foo'] = bar  # AttributeError: This QueryDict instance is immutable
    #...

因为默认的 QueryDict 是不可修改的。解决办法就是复制一份副本,对副本进行修改:

def foobar(request):
    #...
    request.GET = request.GET.copy()  # 添加这一句
    request.GET['foo'] = bar
    #...

参考


Comments