gpt4 book ai didi

python - Django help_text 行长度约定

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:11 24 4
gpt4 key购买 nike

我想知道在将 help_text 和其他硬编码的长行输入 Python/Django 时,行长的约定是什么。我已阅读 PEP-8,其中代码和注释涵盖了行长度,但我不确定这如何适用于长文本字符串。

这是字段“explanation_text”和 help_text 字段选项。

class Question(models.Model):
questionnaire = models.ForeignKey(Questionnaire, on_delete=models.CASCADE)
title = models.CharField(max_length=150, blank=False)
category = models.CharField(max_length=20, blank=False)
created_date = models.DateTimeField(default=datetime.now, blank=True)
explanation_text = models.TextField(
blank=True,
help_text="Explanation text goes here. Candidates will be able to see this after they have taken a questionnaire. To change this, refer to the setting on questionnaire administration. Max length is 1000 characters.",
max_length=1000)

def __str__(self):
return self.title

最佳答案

您可以使用三重引号将 help_text 字符串存储为多行字符串,如下所示:

help_text = """Explanation text goes here. Candidates will be able to see
this after they have taken a questionnaire. To change this,
refer to the setting on questionnaire administration. Max
length is 1000 characters."""

但是,它可能更传统:

  • 将多行字符串存储在 models.py 文件顶部的常量中:

    HELP_TEXT = """Explanation text.....
    ..................
    """

    class Question(...):
    ...
    help_text = HELP_TEXT
  • 将所有常量组合到一个 constants.py 文件中。在 models.py 中,您将拥有:

    import constants

    class Question(...):
    ...
    help_text = constants.HELP_TEXT

关于python - Django help_text 行长度约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35842767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com