【问题标题】:'WSGIRequest' object is not subscriptable in django'WSGIRequest' 对象在 django 中不可下标
【发布时间】:2019-07-23 13:28:29
【问题描述】:

我在views.py 文件的以下函数中收到此错误。我不知道 'WSGIRequest' 是什么或为什么会引发错误。

这里的 settings.MEDIA_ROOT 表示它将列出该目录中的所有文件

def run_existing_query(request):
 context = {}
 print(settings.MEDIA_ROOT)
 context["download"] = ""
 context["list_of_queries"] = os.listdir(settings.MEDIA_ROOT)

 if request.method == "POST": 
    MODULE_DIR = 'settings.MEDIA_ROOT'
    py_file = glob.glob(os.path.join(MODULE_DIR, 
    request['selected_query']+'.py'))
    module_name = pathlib.Path(py_file).stem
    module = importlib.import_module(module_name)
    qe = module.QueryExecutor()  #Query executor is Class name 
    context["download"] = "Hello"
return render(request, "run_existing.html", context)

为什么会出现这个错误?

【问题讨论】:

    标签: python html django


    【解决方案1】:

    您还没有告诉我们错误发生在您的代码中,但我高度怀疑它在 request['selected_query']

    你是subscripting request 对象。但它似乎没有__getitem__ 方法(参见source)。这就是引发错误的原因。

    也许您打算联系request.GET['selected_query'].POST['selected_query']

    【讨论】:

    • 是的。有效。但是您能否再让我知道如何传递下拉值,即 selected_query 是值的字典。 ---------> py_file = glob.glob(os.path.join(MODULE_DIR, request.POST['selected_query']+'.py')) # 在这一行出现错误---- -------->
    • QueryDict.getlist 对您有帮助吗?
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 2020-11-26
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多