Search code examples
djangoobjectrequest

Django: Request object attributs options doesn't show up


I have started first time using django. When creating a function view, I wanted to see what are the atributes of the object request (method, etc) and I excpected that VSC will show all options for the request.option. But the request object doesn't show anything.

I wonder if it is something of the configuration of extention for django to help with autocomplete or showing options, or it is that that is a normal thing.

def index(request): print( request.) return HttpResponse("Hello, World!")

I take this code as an exemple of what I would like to see, and i expected that vsc will show a list of options to see the properties of request object:

def xyz(request): item1 = request.GET['item1'] user = request.user

Per exemple, if I do request.me the autocomplete do something like that about the class request:

def index(request): request.(self, *args, **kwargs): return super().(*args, **kwargs)

Of course I don't want this, but I would like to see the options and the autocomplete of the request object. For exemple, to check if the request is a GET or POST methode.

I would like to know if that is about the configuration of VSC when working with django, or is a normal behavoir.

I have been looking different places but I have found nothiing about this. I would like to know I there is something in the settings of vsc I have to solve or is a normal behavoir.


Solution

  • That is because Django does a bit too much meta-programming for an IDE to understand. You can drop a hint however with:

    from django.http import HttpRequest
    
    
    def index(request: HttpRequest):
        # …
        pass

    Most IDEs will then understand that this is a HttpRequest object, and thus help with autocomplation.

    But still, Python is a very dynamic language, and middleware can add attributes dynamically, so autocompletion in Python is not very convenient. IDEs like PyCharm have editions where they "injected" Django knowledge to make it more effective, but this is more "tricks" than analyzing the meta-programming.