是否可以在类内调用execute_from_command_line(sys.argv)?

时间:2019-11-28 09:37:33

标签: django python-3.x

如何在内部manage.pyInitialize类的实例中execute_from_command_line(sys.argv)进行调用?

import os
import sys

class InitializeService():
    def __init():
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'RMT.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(sys.argv)

InitializeService()

我在运行时遇到此错误:“ python3 manage.py runserver”

  

django.core.exceptions.ImproperlyConfigured:请求的设置为REST_FRAMEWORK,但未配置设置。在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。

我必须在哪里插入此类? 谢谢

0 个答案:

没有答案