Django 2 表单 is_valid 即使在有效的表单数据上也不起作用
Django 2 form is_valid is not working even on valid form data
我正在为 Django(2) 中的 post 数据开发一个非常简单的表单,但总是将表单视为无效,因为 form.is_valid()
没有被调用。
这是我目前的情况:
来自 models.py
:
class UserGroup(models.Model):
email = models.EmailField(primary_key=True)
group = models.CharField(max_length=250, default='notingroup')
def __str__(self):
return self.group
来自 forms.py
:
class UserGroupForm(forms.ModelForm):
class Meta:
model = UserGroup
fields = ('group', 'email')
来自 views.py
:
def group_name(request):
error = ''
if request.method == 'POST':
print(request.POST['email'])
group_form = UserGroupForm(request.POST)
print(group_form)
if group_form.is_valid():
ug_obj = UserGroup()
ug_obj.group = group_form.cleaned_data['group']
ug_obj.email = group_form.cleaned_data['email']
ug_obj.save()
return JsonResponse({"message": 'Got it inside valid'})
else:
error = 'Something went wrong'
print(error)
return JsonResponse({"message": 'an error occurs!'})
来自 html template
:
<form method="post" id="gitForm" action="javascript:call_my_form()">
{% csrf_token %}
<label>Groupname: </label>
<input id="user_email" type="text" value="{{ user.email }}" hidden>
<input id="input" type="text" class="">
<input type="submit" value="Mehet" class="btn btn-primary">
<div id="error" style="color:red"></div>
</form>
来自 javasctip function for Ajax
:
function call_my_form() {
$(document).on('submit', '#gitForm', function (e) {
e.preventDefault();
console.log($('#input').val());
console.log($('#user_email').val());
$.ajax({
type: 'POST',
url: '/groupname',
data: {
group: $('#input').val(),
email: $('#user_email').val(),
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
},
success: function (jsonResponse) {
document.getElementById("demo").innerHTML = jsonResponse.message;
}
}).done(function (jsonResponse) {
document.getElementById("demo").innerHTML = jsonResponse.message;
})
});
}
我正在将正确的数据获取到视图中,但仍然 form.is_valid()
没有被调用,
这是输出:
**********@mail.com
<tr><th><label
for="id_group">Group:</label></th><td><input type="text" name="group"
value="scsacas" maxlength="250" required id="id_group"></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><ul
class="errorlist"><li>User group with this Email already
exists.</li></ul><input type="email" name="email"
value="abdul12391@gmail.com" maxlength="254" required
id="id_email"></td></tr>
Something went wrong [18/Jul/2019 09:47:47]
"POST /groupname HTTP/1.1" 200 31
如输出中所示:User group with this Email already exists.
所以您正在尝试提交一封已存在的电子邮件。这就是为什么你的 form.is_valid()
没有被调用。
我正在为 Django(2) 中的 post 数据开发一个非常简单的表单,但总是将表单视为无效,因为 form.is_valid()
没有被调用。
这是我目前的情况:
来自 models.py
:
class UserGroup(models.Model):
email = models.EmailField(primary_key=True)
group = models.CharField(max_length=250, default='notingroup')
def __str__(self):
return self.group
来自 forms.py
:
class UserGroupForm(forms.ModelForm):
class Meta:
model = UserGroup
fields = ('group', 'email')
来自 views.py
:
def group_name(request):
error = ''
if request.method == 'POST':
print(request.POST['email'])
group_form = UserGroupForm(request.POST)
print(group_form)
if group_form.is_valid():
ug_obj = UserGroup()
ug_obj.group = group_form.cleaned_data['group']
ug_obj.email = group_form.cleaned_data['email']
ug_obj.save()
return JsonResponse({"message": 'Got it inside valid'})
else:
error = 'Something went wrong'
print(error)
return JsonResponse({"message": 'an error occurs!'})
来自 html template
:
<form method="post" id="gitForm" action="javascript:call_my_form()">
{% csrf_token %}
<label>Groupname: </label>
<input id="user_email" type="text" value="{{ user.email }}" hidden>
<input id="input" type="text" class="">
<input type="submit" value="Mehet" class="btn btn-primary">
<div id="error" style="color:red"></div>
</form>
来自 javasctip function for Ajax
:
function call_my_form() {
$(document).on('submit', '#gitForm', function (e) {
e.preventDefault();
console.log($('#input').val());
console.log($('#user_email').val());
$.ajax({
type: 'POST',
url: '/groupname',
data: {
group: $('#input').val(),
email: $('#user_email').val(),
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
},
success: function (jsonResponse) {
document.getElementById("demo").innerHTML = jsonResponse.message;
}
}).done(function (jsonResponse) {
document.getElementById("demo").innerHTML = jsonResponse.message;
})
});
}
我正在将正确的数据获取到视图中,但仍然 form.is_valid()
没有被调用,
这是输出:
**********@mail.com
<tr><th><label for="id_group">Group:</label></th><td><input type="text" name="group" value="scsacas" maxlength="250" required id="id_group"></td></tr> <tr><th><label for="id_email">Email:</label></th><td><ul class="errorlist"><li>User group with this Email already exists.</li></ul><input type="email" name="email" value="abdul12391@gmail.com" maxlength="254" required id="id_email"></td></tr>
Something went wrong [18/Jul/2019 09:47:47] "POST /groupname HTTP/1.1" 200 31
如输出中所示:User group with this Email already exists.
所以您正在尝试提交一封已存在的电子邮件。这就是为什么你的 form.is_valid()
没有被调用。