我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.core.management.ManagementUtility()。
def main(): """ django-develop CLI entry point. """ # XXX: Bail out early if being invoked for autocompletion. utility = ManagementUtility() utility.autocomplete() if not utils.is_inside_virtual_env(): _fail('Run django-develop inside a virtualenv') dd = _get_DjangoDevelop() if not dd.instance_path.exists(): _fail('django-develop not configured, try "django-develop-config"') else: # Set up and hand over to Django dd.activate_dev_settings() utility.execute()
def create_project(project_name, legacy=False): """Create the project using the Django startproject command""" print("Creating a Wagtail project called {project_name}".format(project_name=project_name)) import wagtailstartproject wagtailstartproject_path = os.path.dirname(wagtailstartproject.__file__) if legacy: template_dir = 'legacy_project_template' else: template_dir = 'project_template' template_path = os.path.join(wagtailstartproject_path, template_dir) # Call django-admin startproject utility_args = ['django-admin.py', 'startproject', '--template=' + template_path, '--extension=py,ini,html,rst,json,cfg', project_name] # always put the project template inside the current directory: utility_args.append('.') utility = ManagementUtility(utility_args) utility.execute() print("Success! {project_name} has been created".format(project_name=project_name))
def run(self): command = ManagementUtility().fetch_command("ping") command.execute()
def create_project(args): """ Create a new django project using the longclaw template """ # Make sure given name is not already in use by another python package/module. try: __import__(args.project_name) except ImportError: pass else: sys.exit("'{}' conflicts with the name of an existing " "Python module and cannot be used as a project " "name. Please try another name.".format(args.project_name)) # Get the longclaw template path template_path = path.join(path.dirname(longclaw.__file__), 'project_template') utility = ManagementUtility(( 'django-admin.py', 'startproject', '--template={}'.format(template_path), '--ext=html,css,js,py,txt', args.project_name )) utility.execute() print("{} has been created.".format(args.project_name))
def create_project(parser, options, args): # Validate args if len(args) < 2: parser.error('Please specify a name for your Arctic installation') elif len(args) > 3: parser.error('Too many arguments') project_name = args[1] try: dest_dir = args[2] except IndexError: dest_dir = '' # Make sure given name is not already in use by another # python package/module. try: __import__(project_name) except ImportError: pass else: parser.error('"{}" conflicts with the name of an existing ' 'Python module and cannot be used as a project ' 'name. Please try another name.'.format(project_name)) print('Creating an Arctic project named {}'.format(project_name)) # Create the project from the Arctic template using startapp # First find the path to Arctic import arctic arctic_path = os.path.dirname(arctic.__file__) template_path = os.path.join(arctic_path, 'project_template') # Call django-admin startproject utility_args = ['django-admin.py', 'startproject', '--template=' + template_path, '--ext=html,rst', project_name] if dest_dir: utility_args.append(dest_dir) utility = ManagementUtility(utility_args) utility.execute() # add execute permission to manage.py, somehow it gets lost on the way manage_py = os.path.join(dest_dir or project_name, 'manage.py') st = os.stat(manage_py) os.chmod(manage_py, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) print('Congratulations! {0} has been created.\n' 'The next steps are:\n' '- In config/settings.py change the database settings (if needed).\n' '- Run database migrations: {0}/manage.py migrate.\n' '- Create an admin user: {0}/manage.py createsuperuser.\n' '- Finally run the project: {0}/manage.py runserver.\n' .format(project_name))