Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Django writes custom manage.py commands

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you Django how to write custom manage.py commands, I hope you will learn something after reading this article, let's discuss it together!

Python manage.py commands we use a lot, if we want to use some specific longer commands, such as: python manage.py test-- keepdb-- settings=appname.test_settings (command function: running the project test, using the specified configuration file), then each run is very troublesome, you can set custom manage.py command to achieve.

Operation steps: step 1: create a new custom command module

In an existing project, under the project app you want to set, add the management/commands directory and create a new python module. The module name cannot start with "_", otherwise the module will not be set as a custom command. The custom command set is the name of the module, and the created directory tree is as follows:

Some app under the appname/ # django project

_ _ init__.py

Models.py

Management/

Commands/

_ private.py # this module will not be added to custom commands

Newtest.py # this module will be set to a custom command, and the command will be the module name

Tests.py

Views.py

Note: the app for adding custom commands needs to be registered in `settings.py`installed _ APPS.

Step 2: write custom command functions

Set the functions to be implemented by newtest.py, and the contents of newtest.py:

Import os

Import traceback

From django.core.management.base import BaseCommand, CommandError

Class Command (BaseCommand):

Def add_arguments (self, parser):

Parser.add_argument (

Dest='appname', # parameter name

Type=str, # parameter type

Which app', # help information does help=' test?

)

Def handle (self, * args, * * options):

Try:

Shell_info = 'python manage.py test% s-keepdb-settings=appname.test_settings'% (options [' appname'])

Os.system (shell_info)

Self.stdout.write (self.style.SUCCESS ('command% s executed successfully, argument% s' (_ _ file__, options ['appname'])

Except:

Self.stdout.write (traceback.format_exc ())

Self.stdout.write (self.style.ERROR ('command execution error'))

The above function explains: when python manage.py newtest appname is executed, the test test is invoked and the specified test-specific settings.py configuration is run.

Note: to print the output information, use: `output ("Unterminated line", ending='') `. The `ending='' parameter specifies the end of the output information. If you do not configure the parameter, it will be a newline character by default. `

After reading this article, I believe you have some understanding of "how to write custom manage.py commands in Django". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report