Django帮助文本行长度约定
我想知道在向Python/Django输入help_text和其他硬编码的长行时,行长度约定是什么。我已经阅读了PEP-8,其中行长被覆盖了代码和评论,但我不确定这是如何适用于长字符串的文本。Django帮助文本行长度约定
这是字段'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
额外的“帮助”文本与表单控件来显示。即使您的字段没有在表单上使用,也可以使用 作为文档。
请注意,该值在自动生成的 表单中未被HTML转义。如果您愿意,可以在help_text中包含HTML。例如: 示例:
help_text =“请使用以下格式:YYYY-MM-DD。”
或者,您可以使用纯文本和django.utils.html.escape()至 转义任何HTML特殊字符。确保您逃脱可能来自不受信任的用户的任何帮助 文本,以避免发生跨站点 脚本攻击。
https://docs.djangoproject.com/en/1.9/ref/models/fields/#help-text
没有为它没有规则它仅用来提供额外的信息到用户/开发者(行长度要求是在移动设备和桌面例如不同)
作为maazza说,没有公约。
就我而言,我喜欢使用python implicit string concatenation。
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)
使用ugettext时,其中一期工程干净:
from django.utils.translation import ugettext_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个字符的建议 – maazza
非常感谢,我不能upvote,因为我没有足够高的声誉。我认为,实际上你给出的第一个例子是我将要做的,因为它会更整齐地适应其余的项目。 –
很高兴帮助! – gtlambert
当浏览器中的_displayed_时,我来这里寻找'help_text'是多行的。用HTML'
'标签得到了。例如:'日期格式:DD/MM/YYYY。
时间格式:HH:MM(24小时)' – amolbk