【问题标题】:'WSGIRequest' object is not subscriptable'WSGIRequest' 对象不可下标
【发布时间】:2011-08-20 20:18:14
【问题描述】:

我在views.py 文件中的这个函数中遇到了这个错误。这很令人困惑,因为我不知道“WSGIRequest”是什么,也不知道它为什么会给我带来问题。我知道我有一个名为“newUser”的变量,因为当我取出那一行时, print(request.POST) 行会将其打印出来。

def AddNewUser(请求):

a=AMI()
if(request.method == "POST"):
    print(request.POST)
    print(request["newUser"])
csrfContext = RequestContext(request)
return render_to_response("ac/AddNewUser.html", csrfContext)

`

为什么会出现这个错误?

【问题讨论】:

    标签: python html django


    【解决方案1】:

    It means that WSGIRequest does not implement __getitem__。您正试图将 HttpRequest 对象视为字典,但事实并非如此。如果你想访问这个 newUser 变量,请使用 POST 对象,它实现了一个类似字典的接口:

    request.POST['newUser']
    

    You'd do well to peruse the Django docs in situations like this.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2013-09-22
      • 2020-02-17
      • 2021-11-10
      • 2019-05-16
      • 2016-07-20
      相关资源
      最近更新 更多