Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Django 基本教學 - 從無到有 Django-Beginners-Guide 📝

License

NotificationsYou must be signed in to change notification settings

twtrubiks/django-tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

這邊紀錄如何客製化 management commands

教學

將這個 management commands 放到 musics 資料夾底下,

資料夾結構如下

musics/__init__.pymodels.pymanagement/__init__.pycommands/__init__.pywelcome.pytests.pyviews.py

然後記得一定要將 musics 加入到INSTALLED_APPS 底下,

否則 django 不會偵測.

INSTALLED_APPS= [    ......"musics",]

welcome.py 程式碼如下,

fromdjango.core.management.baseimportBaseCommandclassCommand(BaseCommand):# python3 manage.py help welcomehelp='hello django custom management commands'defadd_arguments(self,parser):# Positional argumentsparser.add_argument("name",type=str)defhandle(self,*args,**kwargs):msg=f'handle - hello django custom management commands:{kwargs["name"]}'self.stdout.write(self.style.SUCCESS("success"))self.stdout.write(msg)

使用方法也很簡單, 先來測試一下是否有正確載入,

如果有出現 welcome 代表成功,

❯ python3 manage.py help[musics]    welcome

接著來測試一下指令

❯ python3 manage.py welcome testsuccesshandle - hello django custom management commands: test

如果你想 透過 vscode debug django custom management commands,

可參考設定 Django Shell 中斷點.

如果想定期執行, 可以搭配Linux 指令教學-Crontab 來執行定期需要執行的內容.

執行環境

  • Python 3.9

Reference

Donation

文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡:laughing:

alt tag

贊助者付款

License

MIT license

About

Django 基本教學 - 從無到有 Django-Beginners-Guide 📝

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp