我无法弄清楚为什么我的表单返回 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 的页面或具有该表单的页面上传递什么视图
抱歉,如果这令人困惑-如果没有意义,请告诉我,我会详细说明
这是我的表格。错误打印
<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>
凯蒂