来自Python脚本的Django Runserver [英] Django runserver from Python script

查看:91
本文介绍了来自Python脚本的Django Runserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动Django服务器的正常方法是从终端或bash脚本运行以下命令:

The normal way to start the Django server is to run the following command from a terminal or a bash script:

python manage.py runserver [Ip.addr]:[port] 

例如

python manage.py runserver 0.0.0.0:8000

如何从Python脚本启动Django服务器?

How can I start a Django server from a Python script?

以下是一个选择

import os
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
    from django.core.management import execute_from_command_line
    args = ['name', 'runserver', '0.0.0.0:8000']
    execute_from_command_line(args)

有更好的方法吗?

推荐答案

Python已经内置了HTTP服务器

Python has already builtin HTTP server

python -m SimpleHTTPServer

或者您还可以运行django服务器

OR you have an alternative to run the django server

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")

from django.core.management import call_command
from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application()
call_command('runserver',  '127.0.0.1:8000')

这篇关于来自Python脚本的Django Runserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆