2017-05-26 96 views
0

我的Django表單要求一個特定的數字(在這種情況下是1345)。但是,如果用戶鍵入其他數字,則會出現警告消息,指示允許的整數的上限/下限。我如何防止這些特定的警告消息?對錯誤輸入的迴應應始終爲:「錯誤」。實現這種行爲的最簡單方法是什麼?Django形式警告消息

#models.py 
class Player(): 
    code = models.PositiveIntegerField(min=1345,max=1345) 
    #etc 

#template.html 
    {% formfield player.code with label="What is the code?" %} 
+0

在您的表單上發佈代碼。 – doru

回答

1

可以顯示error_messages

from django.utils.translation import gettext_lazy as _ 

class PlayerForm(ModelForm): 
    class Meta: 
     model = Player 
     fields = "__all__" 

     error_messages = { 
      'code': { 
       'max_value': _("You have to enter below the 1345."), 
       'min_value': _("You have to enter above the 1345."), 

      } 
     } 

所以,現在你應該明白我的意思。只要你想要的文字。