我想知道在将 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/

    10-09 07:42