I am currently using the package 'django-two-factor-authentication' in my Django project and I see the management commands here (https://django-two-factor-auth.readthedocs.io/en/1.14.0/management-commands.html)
My client would like to have a specific message for users who have not enabled two factor authentication. How would I call this from a view to check their status?
I tried this already:
from django.core.management import call_command
authed = call_command('two_factor_status [email protected]')
print('authed response')
print(authed)
but I get an error message saying that this is an unknown command. If I leave the email out the page doesn't crash and I don't get an error but it prints out "None".
***Update With this specific package you can check if the user has enabled two factor authentication with
request.user.is_verified
I would still like to know if there is a way to call management commands from a view when they are from a third party package though.
You have to split the command and arguments. Please notice, that you will only get the value the
handle()method of the command returns, if any, and not the output which will be printed if you execute the command.I would recommend not to call the command but use the logic directly in the view. This reduces the overhead and allows more granular handling of the response.