Django help_text 行长度约定

Django help_text line length convention

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

Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.

Note that this value is not HTML-escaped in automatically-generated forms. This lets you include HTML in help_text if you so desire. For example:

help_text="Please use the following format: YYYY-MM-DD."

Alternatively you can use plain text and django.utils.html.escape() to escape any HTML special characters. Ensure that you escape any help text that may come from untrusted users to avoid a cross-site scripting attack.

https://docs.djangoproject.com/en/stable/ref/models/fields/#help-text

它没有规则,因为它仅用于向 user/developer 提供额外信息(例如,移动端和桌面端的行长度要求不同)

正如马扎所说,没有约定俗成。

就我而言,我喜欢使用 python 隐式字符串连接。

class Question(models.Model):
    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)

它在使用 gettext 时运行得很干净:

from django.utils.translation import gettext_lazy as _

class Question(models.Model):
    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)

注:顺便说一句,this his how django do