Reputation: 553
I have a Django project and I would like to create a simple python GUI to allow users to change the host address and port on the go without having to interact with a command prompt. I already have a simple python user interface but how would I go about coding something in python that would be able to run a command such as python manage.py createsuperuser
and fill in the required information without a need to run it in a python script that just calls regular terminal commands such as:
subprocess.call(["python","manage.py", "createsuperuser"])
I don't know if this is something one can do without commands but would be amazing if it is since then I can just implement it into my basic python GUI that would allow users to createsuperuser
, runserver
, makemigrations
, migrate
and even change the default host and port on the go.
Upvotes: 1
Views: 1609
Reputation: 3481
you can use from this code and change each command you want , pay attention if you run this command your shell must be in your django project folder if you want, you can change direction with "CD" command before django run command
from subprocess import Popen
from sys import stdout, stdin, stderr
Popen('python manage.py runserver', shell=True, stdin=stdin, stdout=stdout ,stderr=stderr)
you can communicate with shell by this code :
from subprocess import Popen, PIPE
from sys import stdout, stdin, stderr
process = Popen('python manage.py createsuperuser', shell=True, stdin=PIPE, stdout=PIPE ,stderr=stderr)
outs, errs = process.communicate(timeout=15)
print(outs)
username="username"
process.stdin.write(username.encode('ascii'))
process.stdin.close
Unfortunately, for createsuperuser you get this error :
Superuser creation skipped due to not running in a TTY. You can run
manage.py createsuperuser
in your project to create one manually
You can't create super user with tty for security problems.
I prefer :
you can use from this code in your project for create superuser
from django.contrib.auth.models import User;
User.objects.create_superuser('admin', '[email protected]', 'pass')
if you want create super user with shell I would suggest running a Data Migration stackoverflow.com/a/53555252/9533909
Upvotes: 5