django - urlinput 小部件的自定义错误消息

django - custom error message for urlinput widget

django 1.7 尝试自定义在模型表单上触发的默认客户端验证消息。特别是 urlinput 小部件的错误消息(urlfield 的默认设置)。此错误似乎来自提交表单时的客户端验证。

我想将 "Please enter a URL" 更改为其他内容。感觉找遍了都找不到

谁能帮我指明正确的方向?

在Html5中输入url会显示所需的属性信息,如

Cross browser verbiage:
    Firefox: Please enter a URL.
    Chrome: Please enter a URL.
    IE: You must enter a valid URL

您可以使用<a href="https://html.spec.whatwg.org/multipage/forms.html#dom-cva-setcustomvalidity" rel="nofollow">setcustomvalidity</a>

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
    var elements = document.getElementsByTagName("INPUT");
    for (var i = 0; i < elements.length; i++) {
        elements[i].oninvalid = function(e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("This field cannot be left blank");
            }
        };
        elements[i].oninput = function(e) {
            e.target.setCustomValidity("");
        };
    }
})
</script>