【问题标题】:Django form warning messagesDjango 表单警告消息
【发布时间】:2017-10-27 01:30:43
【问题描述】:

我的 Django 表单要求输入一个特定的数字(在本例中为 1345)。然而,如果用户键入其他数字,则会出现一条警告消息,指示允许的整数的上限/下限。如何防止这些特定的警告消息?对错误输入的响应应始终为:“错误”。实现这种行为的最简单方法是什么?

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

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

【问题讨论】:

  • 将代码发布到您的表单中。

标签: django forms templates


【解决方案1】:

你可以显示@987654321@

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."),

            }
        }

所以现在你应该明白了。随心所欲地输入文字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2019-10-03
    • 2015-08-07
    • 2019-05-02
    相关资源
    最近更新 更多