weixin_33691817 2015-10-03 12:25 采纳率: 0%
浏览 140

Django-Ajax:表单提交

I have a problem with a form submission :

My script JS :

function submit_form(){
    var form = $("#theform").serializeArray();
    var the_players = document.getElementsByName("theplayers");
    var tab_players = [];
    for(variable of the_players){
        var name = variable.id.split("_")[1];
        tab_players.push(variable.value);
        }

    // *** I send a tab and THE FORM ***
   $.post("/myview_players",{"theplayers[]": tab_players,"form[]":form}).done(function(data){window.location.reload();}); 

   return false;
}

And the view (url : 'myview_players') :

...
if request.method == "POST":    
    requete = request.POST
    if requete.is_ajax() :
        form = playersForm(requete.get("form"))

        if form.is_valid() :
            print("Valid ! ")
        else :
            print("Not valid...")
        ...

I pass in argument to the playersForm juste a form sent by Ajax. In the terminal, the message "Not valid" is showed... Why ?

  • 写回答

2条回答 默认 最新

  • weixin_33725272 2015-10-03 15:25
    关注

    Change requete to request in your view. Spellcheck?

    Also, in $.post, if you pass as "form[]":form, the view would expect a list. Try doing "form":form

    评论

报告相同问题?