Jquery 使用http响应提交Ajax中的Django模型表单错误

Jquery 使用http响应提交Ajax中的Django模型表单错误,jquery,python,ajax,django,Jquery,Python,Ajax,Django,我在将表单提交到后端时遇到了麻烦。 当我单击submit时,它转到someform()函数的error函数。 在视图中,我在这一行的crave=get_object_或_404(CraveData,pk=id)处出错 views.py @login_required def save_post(request, id): print "you are in main" if request.is_ajax(): print "you are in ajax"

我在将表单提交到后端时遇到了麻烦。 当我单击submit时,它转到someform()函数的error函数。 在视图中,我在这一行的crave=get_object_或_404(CraveData,pk=id)处出错

views.py

@login_required   
def save_post(request, id):
    print "you are in main"
    if request.is_ajax():
        print "you are in ajax"
        crave = get_object_or_404(CraveData, pk=id)
        print "after crave"
        check = Comment.objects.filter(crave=crave)
        print "after check"
        form=CraveReplyForm()
        print "after form"
        if request.method=="POST":
            form=CraveReplyForm(request.POST, request.FILES)
            if form.is_valid():
                print "Gaurav"
                reply = form.save(commit=False)               
                reply.crave = crave             
                reply.comment_owener=request.user
                print "akash"
                print reply
                reply.save()
                html= render_to_string("crave/crave.html", {'crave': crave, 'form' : form, 'check':check})
                response = json.dumps({'html':html})
                print response              
                return HttpResponse(response, mimetype="application/json")
            else:

                print "ohh you in else"
                response= {}
        else:
            response= {}
            return HttpResponse(response, mimetype="application/json")
在模板crave.html中

    <li><a href="/crave/save_post/{{crave_made.id}}" onclick='return showDiv()'>Reply</a></li>
 <div class="row" style="display: none;" id="showme2">
  <div class="large-2 columns small-3"><img src="http://placehold.it/80x80&text=[img]" /></div>
  <div class="large-10 columns">
    <p><strong>{{user.username}} said:</strong>{{crave}}</p>
    <ul class="inline-list">
      {% for pf in check%}
      {{pf.reply}}<br>
      {% endfor %}

      <form class="horizontal-form" role="form" action="/crave/save_post/{{crave_made.id}}" id="showme1" method="post"  style="padding: 10px;" >
        {% csrf_token %}
        <div class="form-group" >
          <div class="col-sm-10">
            {{ form.reply.label_tag }} {{ form.reply }} </br> </br>
          </div>
        </div>
        <input type="submit"  class="btn btn-success" value="reply" />
      </form>
      <li><a href="">Share</a></li>
    </ul>
  </div>
</div>
<script>
function showDiv() {
                       $("#showme2").show();

                        return false;
                    }
function someform() {
        $.ajax({
            type: "POST",
            data: "",
            url: "/crave/save_post/{{crave_made.id}}",
            success: function (response) {
                alert(response)
                //$('.ajaxProgress2').html(response.html);
            },
            error: function () {
                alert('some error');
            }
        });

        return false;
    }
</script>
  • {{user.username}说:{{{crave}}

      {检查%中的pf的百分比} {{pf.reply}}
      {%endfor%} {%csrf_令牌%} {{form.reply.label_tag}{{form.reply}}

    函数showDiv(){ $(“#showme2”).show(); 返回false; } 函数someform(){ $.ajax({ 类型:“POST”, 数据:“, url:“/crave/save_post/{{crave_made.id}”, 成功:功能(响应){ 警报(响应) //$('.ajaxProgress2').html(response.html); }, 错误:函数(){ 警报(“某些错误”); } }); 返回false; }
    url.py

    url(r'^save_post/(?P<id>.*)$', 'save_post'),
    
    url(r'^save\u post/(?P.*),“save\u post”),
    

    我不知道我做错了什么

    您的问题是表单无效,但当表单无效时,您没有处理这种情况

        if request.method=="POST":
            form=CraveReplyForm(request.POST, request.FILES)
            if form.is_valid():
    
                reply = form.save(commit=False)               
                reply.crave = crave             
                reply.comment_owener=request.user
                reply.save()
    
                response = {}              
                return HttpResponse(response, mimetype="application/json")
    
            #else is missing here <---------------------------------------------
    
        else:
                csrfContext = RequestContext(request)
    
    if request.method==“POST”:
    form=CraveReplyForm(request.POST、request.FILES)
    如果form.is_有效():
    reply=form.save(commit=False)
    渴望,渴望
    reply.comment\u owener=request.user
    答复.保存()
    响应={}
    返回HttpResponse(响应,mimetype=“application/json”)
    
    #此处缺少其他内容,但我的表单正在获取仅验证数据未保存。我没有从表单中获取数据以供查看。正在创建空白对象。