2016-03-07 19 views
3

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

回答

4

你可以使用三引號如下存儲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 
    
+1

非常感謝,我不能upvote,因爲我沒有足夠高的聲譽。我認爲,實際上你給出的第一個例子是我將要做的,因爲它會更整齊地適應其餘的項目。 –

+0

很高興幫助! – gtlambert

+0

當瀏覽器中的_displayed_時,我來這裏尋找'help_text'是多行的。用HTML'
'標籤得到了。例如:'日期格式:DD/MM/YYYY
時間格式:HH:MM(24小時)' – amolbk

2

額外的「幫助」文本與表單控件來顯示。即使您的字段沒有在表單上使用,也可以使用 作爲文檔。

請注意,該值在自動生成的 表單中未被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

沒有爲它沒有規則它僅用來提供額外的信息到用戶/開發者(行長度要求是在移動設備和桌面例如不同)

2

作爲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

+1

不錯的提示,它符合PEP-8 80個字符的建議 – maazza