startapp.py 文件源码

python
阅读 112 收藏 0 点赞 0 评论 0

项目:django-template 作者: DheerendraRathor 项目源码 文件源码
def handle(self, **options):
        directory = options.get('directory', None)
        # directory key is already present in options which is None.
        name = options['name']

        full_path = None
        if not directory:
            """
            If directory is not provided then use DEFAULT_APPS_DIRECTORY.

            This block creates DEFAULT_APPS_DIRECTORY/app_name directory and
            pass that path to default startapp command.
            """
            directory = os.path.join(settings.BASE_DIR, settings.DEFAULT_APPS_DIRECTORY)
            full_path = os.path.join(directory, name)
            if not os.path.exists(full_path):
                os.makedirs(full_path)
            options['directory'] = full_path

        try:
            super().handle(**options)
            if full_path:
                """
                If apps directory is used then change the app name to DEFAULT_APPS_DIRECTORY.app_name
                in AppConfig
                """
                apps_py = os.path.join(full_path, 'apps.py')
                if os.path.isfile(apps_py):
                    for line in fileinput.input(apps_py, inplace=True):
                        line = line.replace("'%s'" % name, "'%s.%s'" % (settings.DEFAULT_APPS_DIRECTORY, name))
                        print(line, end='')
        except CommandError:
            if full_path:
                shutil.rmtree(full_path)
            raise
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号