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

时间:2014-05-01 14:05:33

标签: jquery python ajax django

我很难将表单提交给后端。 当我点击提交时,它会转到someform()函数的错误函数。 并且在视图中我在crave = get_object_or_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>

urls.py

url(r'^save_post/(?P<id>.*)$', 'save_post'),

我不知道我在做什么错误

1 个答案:

答案 0 :(得分:-1)

您的问题是该表单无效,但在表单无效时您没有处理该情况

    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)