我一直在用Django开发应用程序,它有许多表单,每个表单位于不同的页面中。在第一个视图中,我只是向用户询问一个简单的信息,它被发送到python并用于一些公式。我以前执行此操作的代码如下所示,并且运行平稳:
<html>
<head>
<title>Data</title>
</head>
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'app/content/bootstrap.css' %}">
<link rel="stylesheet" href="{% static 'app/content/custom.css' %}">
<script src="{% static 'app/scripts/jquery-1.10.2.min.js' %}"></script>
<script src="{% static 'app/scripts/jquery.validate.js' %}"></script>
<script src="{% static 'app/scripts/jquery.maskedinput.min.js' %}"></script>
<script src="{% static 'app/scripts/custom.js' %}"></script>
{% if error == 404 %}
<label>Who are you? Go away!</label>
<br>
{% endif %}
Welcome! Give me your data!
<body>
<form action="/search/" method="post" id="myPrettyForm">
{% csrf_token %}
<input type="text" name="q" id="q">
<input type="submit" value="Go!">
</form>
</body>
<script type="text/javascript">
$("#q").mask("00000000000");
</script>
</html>
如我所说,这段代码运行良好。但是,我想添加一个函数,在该函数中,一旦单击“提交”,将
q
输入字段被禁用。为此,我尝试将这段代码添加到上面的脚本中:
$("#myPrettyForm").submit(function () {
$("#myPrettyForm:input").prop("disabled", true);
});
但是,当我将此代码段添加到代码中时,每次尝试提交表单时,我都会收到一个“
禁止(403)CSRF验证失败。请求已中止。
“错误。根据
this
,此错误通常与没有
{% csrf_token %}
元素。但是,我的窗体已经有了这个元素。
那我在这里做错什么了?如果我使用了错误的方法来阻止我的用户在提交表单后写入输入,那么其他方法可以
我用什么?