I currently running a Django application and want to provide a completely different service using Python running on the same server. This won't be a web service.
For this I also want to set up some level of authentication, and kind of want to use the credentials from the Users table in the Django application.
I'm very close to the solution, but not getting a clean response yet:
import os, sys
import Dashboard.settings
from django.contrib.auth import authenticate
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Dashboard.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line()
user = authenticate(username="testuser", password="testpassword"
)
if user is not None:
print('Validated')
else:
# No backend authenticated the credentials
print('wrong credentials')
This does work, however, my output is the manage.py help menu followed by the output I want.
Is there another command than execute_from_command_line(sys.argv) which I can use to mute the help menu?
...
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
Validated
By adding this to the code:
import django
django.setup()
Apps are loaded, and I don't need execute_from_command_line() anymore.