jquery / ajax帖子列表最后以[]发布

时间:2018-06-01 17:41:27

标签: jquery ajax django

我发布一个jquery / ajax,django结束发送一个字典 但是我的观点是我得到了这样的区域{'区域[]':[' 4',' 5'],... }形成它的确定但是要获得密钥的区域我必须使用'区域[]' 我怎样才能得到像这样的区域'没有括号? 如果我尝试使用form.is_valid()中的变量区域,它将变为None 为什么会这样? 谢谢

convertTo-json $Response | Out-file -FilePath "C:\SQL\response3.txt"

我的观点

     var application_area = "/api/application-area/"


    $.getJSON(application_area, function(area){
        try{

            (function ($, undefined) {
                $.jstree.defaults.conditionalselect = function () { return true; };

                $.jstree.plugins.conditionalselect = function (options, parent){                                                                         
                    this.activate_node = function (obj, e) {                                                  
                        if(this.settings.conditionalselect.call(this, this.get_node(obj))) {                            
                            parent.activate_node.call(this, obj, e);
                        }        
                    };
                };

            })(jQuery);               
            createJSTree(area)                
        }catch(ex){
            console.log(ex)        
        }
    });


    function createJSTree(area) {            
        $("#search_tree").keypress(function(){
            var searchString = $(this).val();
            $("#tree").jstree('search', searchString);
        })                      

        $('#tree').jstree({                    
            "conditionalselect" : function (node) {
                if(node.parents.length == 1 || node.parents.lengoh == 2 ||  node.parents.length == 3 ){
                    return false
                }
                else if(node.parents.length == 4){
                    if(this.get_selected().length >= 10){
                        if(this.is_selected(node)){
                            return true
                        }
                        return false
                    }else{
                        return true
                    };
                }                     
            },               
            'core': {
                "multiple": true,  
                "check_callback" : true,              
            "themes": {
                "theme": "classic",
                "dots": true,
                "variant": "small",
                "icons": false,
                "responsive":true,
            },
            'data':area
            },
            "search":{
                "case_insensitive":true,
                "show_only_matches":true,

            },                
            "checkbox": {
                "keep_selected_style": false,
                "three_state" : true,

            },

            "plugins": ["wholerow", "checkbox", "search","conditionalselect", "dnd", "conditionalteste"],
        })                                
    }    

    var list_area  = []

    $("#tree").on("changed.jstree", function(e, node){            
        list_area = node.instance.get_selected(false)
        console.log(list_area)
    })


    $('#documet').ready(function(){

        console.log(list_area)        

        $('#project_form_id').on('submit', function(event){
            //event.preventDefault()

            var form = $(this)                

            //var detail = {
              //  myForm: form.serializeArray(),
                //'areas':list_area 
            //} 


            $.ajax({
                url: "{% url 'project_create' %}",
                method: "POST",
                data:{'areas':list_area},
                dataType: 'json',
                headers: {'X-CSRFToken': '{{ csrf_token }}'},        
                success: function(response){
                    console.log('success')
                    console.log(list_area)
                    $('#project_form_id').reset(); 
                },
                error:function(error){
                    console.log(error)
                    //$("#modal-book .modal-content").html(data.html_form);

                }
            });

        })
    })
    </script>

html表单

    def projectcreate(request):
        researcher = Researcher.objects.get(user_id=request.user.id)          

        if request.method == 'POST':           
        areas = request.POST.getlist('areas[]')               
        print(areas)
        form = ProjectForm(researcher, request.POST or None, request.FILES or None)            


        if form.is_valid():
            print(areas)
            print('true')
            project = Project()

            project.with_ict = form.cleaned_data['with_ict']
            project.with_company = form.cleaned_data['with_company']
            project.for_company = form.cleaned_data['for_company']
            project.predominant_area = form.cleaned_data['']
            project.title = form['title']
            project.ubc = form['ubc']
            project.conclusion_date = form.cleaned_data['conclusion_date']
            project.category = form.cleaned_data['category']
            project.has_patent = form.cleaned_data['has_patent']
            project.tech_transf = form.cleaned_data['tech_transf']
            project.tech_transf_date = form.cleaned_data['tech_transf_date']
            project.stage = form.cleaned_data['stage']
            project.result = form.cleaned_data['result']
            project.researcher  = researcher
            project.save()
            if areas:
            for area in areas:
                project.application_area.add(area)
        else:
            print('false')

        company_form = CompanyForm(request.POST or None, prefix='company')
        if company_form.is_valid():
            company = Company()
            company.name = company_form.cleaned_data['name']
            company.cnpj = company_form.cleaned_data['cnpj']
            company.email = company_form.cleaned_data['email']
            company.save()

        return HttpResponseRedirect(reverse('project_list'))
        else:
        form = ProjectForm(researcher)
        company_form = CompanyForm(prefix='company')
        context = {
            'form':form,
            'company_form':company_form
        }
        html_form = render_to_string('researcher/project/project_form.html',
            context,
            request=request,
        )
        return JsonResponse({'html_form': html_form})

0 个答案:

没有答案