提问人:Rogerio Da Costa Djroger 提问时间:8/6/2022 最后编辑:STerliakovRogerio Da Costa Djroger 更新时间:8/7/2022 访问量:58
尝试在键入时验证 django 表单字段时出现问题
Problem trying to validate django form field while typing
问:
我正在尝试根据另一个问题在输入时验证字段(如何在键入时验证 django 表单字段?
js 不计算验证视图
形式
{{ form.num }}
<p id="validate_number" class="help is-danger is-hidden ">nome já utilizado</p>
js
<script>
$('#id_num').on('input', function () {
var id_number = $(this).val();
$.ajax({
url: '{% url 'validate-num' %}',
data: {
'num': id_number
},
dataType: 'json',
success: function (data) {
if (data.is_taken) {
$("#validate_number").show();
document.getElementById('id_num').style.borderColor = "red";
document.getElementById("submit_change").disabled = true;
}
else {
$("#validate_number").hide();
document.getElementById('id_num').style.borderColor = "#e7e7e7";
document.getElementById("submit_change").disabled = false;
}
}
});
});
</script>
url.py
urlpatterns = [
# ...
path('validatenum/', validate_inventory_number, name='validate-num'),
# ...
]
视图
def validate_inventory_number(request):
number = request.GET.get('num', None)
data = {
'is_taken': InventoryNumber.objects.filter(num=number).exists()
}
return JsonResponse(data)
答: 暂无答案
评论