我将如何为 运行 一个 django 项目创建一个 gui?

How would I go about creating a gui to run a django project?

我有一个 Django 项目,我想创建一个简单的 python GUI 以允许用户随时随地更改主机地址和端口,而无需与命令提示符交互。我已经有了一个简单的 python 用户界面,但是我将如何在 python 中编写一些代码,以便能够 运行 一个命令,例如 python manage.py createsuperuser 并填写所需的内容无需在 python 脚本中 运行 信息,该脚本仅调用常规终端命令,例如:

subprocess.call(["python","manage.py", "createsuperuser"])

我不知道这是否是一个人可以在没有命令的情况下完成的事情,但如果从那时起我就可以将它实现到我的基本 python GUI 中,这将允许用户 createsuperuserrunservermakemigrationsmigrate,甚至随时更改默认主机和端口。

您可以使用这段代码并更改您想要的每个命令,请注意如果您 运行 此命令您的 shell 必须在您的 django 项目文件夹中 如果你愿意,你可以在 django 运行 命令

之前使用 "CD" 命令改变方向
from subprocess import Popen

from sys import stdout, stdin, stderr

Popen('python manage.py runserver', shell=True, stdin=stdin, stdout=stdout ,stderr=stderr)

您可以通过此代码与 shell 通信:

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

不幸的是,对于 createsuperuser,您会收到此错误:

Superuser creation skipped due to not running in a TTY. You can run manage.py createsuperuser in your project to create one manually

由于安全问题,您无法使用 tty 创建超级用户。

我更喜欢:

您可以在您的项目中使用此代码创建超级用户

from django.contrib.auth.models import User; 
User.objects.create_superuser('admin', 'admin@example.com', 'pass')

如果您想使用 shell 创建超级用户,我建议 运行 进行数据迁移 whosebug.com/a/53555252/9533909