2012-05-01 56 views
0

阿賈克斯形式is_valid返回false(Django的)

我有了一個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打印

<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

更多的澄清,的確會的幫助。你可以發佈相關的JS。另外,你應該通過打印'form.errors'來調試它,在ajax錯誤上返回錯誤並將其記錄在JS等中,表單會告訴你它爲什麼無效。 –

+0

感謝你有關form.errors –

+0

這條有用的信息,你能幫我理解我應該通過我的表單 - 我應該通過調用ajax和頁面提交表單或兩者還是什麼?謝謝 –

回答

0

檢查您是否發送每個非空字段。有時候您發送的數據名稱不正確,或者您缺少必填字段。要檢查發生了什麼,我建議使用shell輸入數據並訪問表單的錯誤列表。要做到這一點

$python manage.py shell 
>>> from yourapp.models import yourClass 
>>> data = {'name':'Jefrey','age':27,'home':'NYC'} 
>>> f = yourClass(data) 
>>> f.is_valid() 

如果你假,這樣做

>>>f.errors 

這樣,你會看到是怎麼回事。更多關於在Django文檔 https://docs.djangoproject.com/en/dev/ref/forms/api/

我希望這有助於