Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django对象列表在通过ajax提交对象创建表单后未更新_Ajax_Django_Forms - Fatal编程技术网

django对象列表在通过ajax提交对象创建表单后未更新

django对象列表在通过ajax提交对象创建表单后未更新,ajax,django,forms,Ajax,Django,Forms,我正在处理一个django项目,其中一个模板的弹出div中有一个表单,提交表单以创建新对象 到目前为止,ajax函数已成功提交表单并将配方添加到管理数据库中,但由于某些原因,它没有出现在我传递到模板中的对象列表中 即使我刷新页面,新添加的对象也不会添加到模板中,并且创建的对象在管理页面上仍处于不确定状态 代码 ajax提交功能: 模板: 将为当前用户筛选食谱。你在哪里为新创建的项目设置用户?我不知道我是否在哪里可以这样做-我注意到的另一点是,当我在视图中将菜谱添加到菜谱中时,一切似乎都很好-但这

我正在处理一个django项目,其中一个模板的弹出div中有一个表单,提交表单以创建新对象

到目前为止,ajax函数已成功提交表单并将配方添加到管理数据库中,但由于某些原因,它没有出现在我传递到模板中的对象列表中

即使我刷新页面,新添加的对象也不会添加到模板中,并且创建的对象在管理页面上仍处于不确定状态

代码

ajax提交功能:

模板:


将为当前用户筛选食谱。你在哪里为新创建的项目设置用户?我不知道我是否在哪里可以这样做-我注意到的另一点是,当我在视图中将菜谱添加到菜谱中时,一切似乎都很好-但这似乎使整个ajax毫无意义请展示您的模型。和表格。
<script type="text/javascript"> 
$(document).ready(function(){

    function hijack() {
        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');
                        alert('recipe added');
                    } else {        
                        console.log('failure');
                        var newForm = data.form;
                        form.replaceWith(newForm);
                        hijack();
                    }
                })
            });
            return false;
        });
    };
    hijack();
});
</script> 
   ** //this is the page the has the popup div to load the form**
    def account(request):
        user = request.user
        if request.user.is_authenticated():

            cookbooks = user.cookbooks
            if cookbooks.all().exists():
                cookbook = cookbooks.all()[0]
                form = RecipeForm(initial = {'original_cookbook' : request.user.cookbooks.all()[0]})
                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))


    **//this is the page that handles the ajax and is the action of the form**
    def createrecipe(request):
            if request.method == 'POST':
                form = RecipeForm(request.POST)     
                if form.is_valid():
                    form = form.save(commit=False)
                    form.original_cookbook = request.user.cookbooks.all()[0]
                    form.save()

                    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:
                    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')
    {% block content %}
        <div id="recipe_cont">
                {% for recipe in recipe_list %}// here is where add each recipe in the recipe list
            <div class="recipe">
                <div id="recipebutton{{ forloop.counter }}" class="button"> 
                </div>
            </div>
        {% endfor %}
        </div>

            <div id="popupContact" class="popup">
                <a id="popupContactClose" style="cursor:pointer;float:right;">x</a>
                <p id="contactArea">
                    <div id="create_form_span">
                        {% include 'cookbook/create_form.html' %} // here is the form 
                    </div>
                </p>
            </div>
        </div>

    {% endblock %}


//template for the form that is included
    <head>

    </head>
    <body>
    <form action="{% url cookbook.views.createrecipe %}" method="POST" name="recipeform" id="createrecipeform">
        <table>
            {% csrf_token %}
            {{ form.as_table }}
        </table>
        <p><input type="submit" value="Submit"></p>
    </form>
    </body>