Answer the question
In order to leave comments, you need to log in
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")
def current_user(request):
CU = request.user
print(CU.email)
TypeError: current_user() missing 1 required positional argument: 'request'
path('current_user/', current_user, name='current_user'),
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question