Python Django帮助_文本行长度约定
我想知道在Python/Django中输入help_文本和其他硬编码的长行时,行长的约定是什么。我读过PEP-8,其中代码和注释包含行长度,但我不确定这如何适用于长字符串文本 这是“解释文本”字段和“帮助文本”字段选项的首选项Python Django帮助_文本行长度约定,python,django,conventions,pep8,Python,Django,Conventions,Pep8,我想知道在Python/Django中输入help_文本和其他硬编码的长行时,行长的约定是什么。我读过PEP-8,其中代码和注释包含行长度,但我不确定这如何适用于长字符串文本 这是“解释文本”字段和“帮助文本”字段选项的首选项 class Question(models.Model): questionnaire = models.ForeignKey(Questionnaire, on_delete=models.CASCADE) title = models.CharFiel
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\u文本
字符串存储为多行字符串,如下所示:
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
它没有规则,因为它只用于向用户/开发者提供额外信息(例如,手机和桌面上的线路长度要求不同)正如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)
注:顺便说一句,.非常感谢,我还不能投票,因为我的声望不够高。事实上,我认为你给出的第一个例子是我将选择的,因为它将更适合项目的其余部分。很高兴能提供帮助!我来这里是想让
帮助_text
在浏览器中显示为多行。用HTML
标记得到了它。例如:日期格式:DD/MM/YYYY。
时间格式:HH:MM(24小时)
很好的提示,符合PEP-8 80字符的建议