我无法弄清楚为什么我的表单将false返回给is_valid
我有一个名为account的页面,它具有ajax函数来提交表单
我在我的帐户视图和我的createform视图中传递了表单
createform视图:这是处理我的ajax的地方
def createrecipe(request):
print "entering createrecipeview"
if request.method == 'POST':
print "form is a post"
form = RecipeForm(request.POST)
print form.errors
if form.is_valid():
print "form is valid"
form = RecipeForm(initial = {'original_cookbook' : request.user.cookbooks.all()[0]})
form = form.save()
if form.cleaned_data['reset_recipe'] == "True":
print "reset recipe"
form = RecipeForm(initial = {"original_cookbook": request.user.cookbooks.all()[0]})
t = loader.get_template('cookbook/create_form.html')
c = RequestContext(request, {
'form': form,
})
data = {
'replace': True,
'form': t.render(c),
'success': False,
}
json = simplejson.dumps(data)
return HttpResponse(json,mimetype='text/plain')
t = loader.get_template('cookbook/create_form.html')
c = RequestContext(request, {
'form': form,
})
data = {
'replace': True,
'form': t.render(c),
'success': True,
}
json = simplejson.dumps(data)
return HttpResponse(json, mimetype='text/plain')
else:
print "form is invalid"
form = RecipeForm(request.POST)
t = loader.get_template('cookbook/create_form.html')
c = RequestContext(request, {
'form':form,
})
data ={
'form': t.render(c),
'success': False,
}
json = simplejson.dumps(data)
return HttpResponse(json, mimetype='text/plain')
帐户视图:这是我在ajax视图的表单中传递的地方
def account(request):
user = request.user
if request.user.is_authenticated():
cookbooks = user.cookbooks
if cookbooks.all().exists():
cookbook = cookbooks.all()[0]
form = RecipeForm(request.POST)
recipe_list = cookbook.recipes.all()
else:
raise Http404
else:
return HttpResponseRedirect('/accounts/login')
t = loader.get_template('cookbook/account.html')
c = RequestContext(request, {
'form': form,
'recipe_list': recipe_list
})
return HttpResponse(t.render(c))
所以我的问题是当使用ajax和django时,我需要在哪个视图中传递表单 具有ajax js的页面或具有该表单的页面
抱歉,如果这令人困惑 - 如果没有意义,请告诉我,我会详细说明这是我的form.errors print
<ul class="errorlist"><li>name<ul class="errorlist"><li>This field is required.</li></ul></li><li>ingredients<ul class="errorlist"><li>This field cannot be null.</li></ul></li><li>author<ul class="errorlist"><li>This field is required.</li></ul></li><li>steps<ul class="errorlist"><li>This field cannot be null.</li></ul></li><li>prep_time<ul class="errorlist"><li>This field is required.</li></ul></li><li>type<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
这实际上是有道理的,因为我没有在表格中添加任何输入而没有验证
抱歉,但我相信这是一个误报这是我的js
<script type="text/javascript">
$(document).ready(function(){
var form = $('form#createrecipeform');
form.submit(function(e) {
e.preventDefault();
console.log('ajax form submission function called successfully.');
form = $(this);
console.log(form)
var serialized_form = form.serialize();
$.ajax({ type: "POST",
url: $(this).attr('action'),
data: serialized_form,
success: (function(data) {
console.log('ajax success function called successfully.');
data = $.parseJSON(data);
if (data.success) {
console.log('success');//i dont know what to do here
var newForm = data.form;
form.replaceWith(newForm);
} else {
console.log('failure');// i also don't know what to do here
var newForm = data.form;
form.replaceWith(newForm);
}
})
});
return false;
});
});
</script>
凯蒂
答案 0 :(得分:0)
检查是否发送了每个非空字段。有时您发送的数据属性名称不正确,或者您缺少必填字段。为了检查发生了什么,我建议使用shell输入数据并访问表单的错误列表。要做到这一点
$python manage.py shell
>>> from yourapp.models import yourClass
>>> data = {'name':'Jefrey','age':27,'home':'NYC'}
>>> f = yourClass(data)
>>> f.is_valid()
如果你得到False,请执行此操作
>>>f.errors
这样你就会看到发生了什么。更多关于Django文档的内容 https://docs.djangoproject.com/en/dev/ref/forms/api/
我希望这会有所帮助