【问题标题】:Django help_text line length conventionDjango help_text 行长约定
【发布时间】:2016-06-20 22:42:00
【问题描述】:

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

这是 '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

【问题讨论】:

    标签: python django conventions pep8


    【解决方案1】:

    与表单小部件一起显示的额外“帮助”文本。很有用 用于文档,即使您的字段未在表单上使用。

    请注意,此值在自动生成中不是 HTML 转义的 形式。如果您愿意,这可以让您在 help_text 中包含 HTML。为了 示例:

    help_text="请使用以下格式:YYYY-MM-DD。"

    或者,您可以使用纯文本和 django.utils.html.escape() 来 转义任何 HTML 特殊字符。确保您逃避任何帮助 可能来自不受信任的用户的文本,以避免跨站点 脚本攻击。

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

    没有规定,因为它仅用于向用户/开发人员提供额外的信息(例如,移动设备和桌面设备的行长要求不同)

    【讨论】:

      【解决方案2】:

      正如 maazza 所说,没有约定。

      就我而言,我喜欢使用 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

      【讨论】:

      • 不错的提示,它符合 PEP-8 80 个字符的建议
      【解决方案3】:

      您可以使用三引号将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
        

      【讨论】:

      • 非常感谢,我还不能投票,因为我没有足够高的声誉。我认为实际上您给出的第一个示例是我将使用的示例,因为它更适合项目的其余部分。
      • 很高兴能帮上忙!
      • 我来到这里寻找help_text 在浏览器中显示 时是多行的。使用 HTML <br> 标签得到了它。例如:Date format: <em>DD/MM/YYYY</em>.<br>Time format: <em>HH:MM</em> (24 hours)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2018-11-02
      • 2018-08-20
      • 2016-02-28
      • 2012-12-17
      • 2018-01-14
      相关资源
      最近更新 更多