0

I'm getting this error in the below function in my views.py file. I don't know what 'WSGIRequest' is or why it's throwing an error.

Here the settings.MEDIA_ROOT means it will list all the files in that directory

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)

Why am I getting this error?

Nari Kumar
  • 23
  • 6

1 Answers1

0

You haven't told us where the error occurs in your code, but I highly suspect it's in request['selected_query'].

You are subscripting the request object. But it doesn't seem to have a __getitem__ method (see source). That's why the error is raised.

Maybe you intend to address request.GET['selected_query'] or .POST['selected_query']?

ascripter
  • 5,665
  • 12
  • 45
  • 68
  • Yeah. It worked. But one more could you please let me know how to pass drop down values i.e selected_query is a dict of values. ---------> py_file = glob.glob(os.path.join(MODULE_DIR, request.POST['selected_query']+'.py')) # Getting error here at this line ------------> – Nari Kumar Jul 23 '19 at 14:31
  • Does [`QueryDict.getlist`](https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.QueryDict.getlist) help you? – ascripter Jul 23 '19 at 14:53