Django 命令:如何在帮助文本中插入换行符?

Django command: How to insert newline in the help text?

我想做这样的事情,但是对于 Django 管理命令: Python argparse: How to insert newline in the help text?

来自documentation

You can customize the instance by overriding this method and calling super() with kwargs of ArgumentParser parameters.

通过覆盖 create_parser 方法 您可以设置 ArgumentParserformatter_class:

from argparse import RawTextHelpFormatter
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    def create_parser(self, *args, **kwargs):
        parser = super(Command, self).create_parser(*args, **kwargs)
        parser.formatter_class = RawTextHelpFormatter
        return parser

您可以通过输入 HTML
标签在 help_text 中插入新行

例如

name=models.Charfield(max_length=10, help_text="Enter First name or <br/> Enter full name")