J
J
Jekson2019-12-09 12:26:11
Django
Jekson, 2019-12-09 12:26:11

Request argument in Django functions?

There is a function (not dzhangovskaya) that passes the html file for editing to google doc. One of the parameters there is email, which I now have hardcoded. Just for everyone, I will give the body of the function, although the question is not in it

def file_to_drive(import_file=None):
    credentials = service_account.Credentials.from_service_account_file(ACCOUNT_FILE, scopes=SCOPES)
    service = build('drive', 'v3', credentials=credentials)
    file_metadata = {
        'name': 'CV:' + uuid.uuid4().hex,
        'mimeType': 'application/vnd.google-apps.document'
    }
    media = MediaFileUpload(import_file,
                            mimetype='text/html',
                            resumable=True)

    file = service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
    fileId = file.get('id')
    current_user()
    permission1 = {
        'type': 'user',
        'role': 'writer',
        'emailAddress': '[email protected]',
    }
    service.permissions().create(fileId=fileId, body=permission1).execute()
    permission2 = {
        'type': 'anyone',
        'role': 'writer',
    }
    service.permissions().create(fileId=fileId, body=permission2).execute()
    return (f"https://docs.google.com/document/d/{file.get('id')}/edit")

I want to get the address of the current user to replace my hardcode with. I'm trying the standard approach.
def current_user(request):
    CU = request.user
    print(CU.email)

And I get
TypeError: current_user() missing 1 required positional argument: 'request'

And really, where does request come from in Dzhang views and why is it missing in my case?
I tried to bind the view with url
path('current_user/', current_user, name='current_user'),

But the result is the same

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antonksa, 2019-12-09
@Lepilov

Somehow there is too much magic...
Don't you understand that request doesn't spontaneously spawn inside a function, like children in cabbage?!
1. When the framework starts, djanga searches for all urls.py, subtracts all urlpatterns from them.
2. After that, it saves the link url - function in its internal registry.
3. When a request comes in, WSGI reads TCP data, HTTP data and forms a WSGIRequest object with this information and calls the main djanga handler.
4. The django handler, having received this object, forms its own HttpRequest based on it, supplementing it with dzhang features. After that, a match is found for the url and the function that processes this url is called, with the HttpRequest passed to it. And by the way, not only request. It is necessary to write:

def handler(request, *args, **kwargs) -> HttpResponse:
    pass

because dzhanga can NOT ONLY pass REQUEST to a function, then you will be stupid, "why does my user_id write that it did not fit."
DO NOT "#;%% WRITE blabla(request) IN THE AIR AND WAIT FOR REQUEST TO BE GENERATED FROM NOTHING!!!
In general, I have a strong feeling that you need to put aside Django for three or four months and learn python itself first...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question