【问题标题】:Django Model Form Error in Ajax Submit using http response使用 http 响应提交的 Ajax 中的 Django 模型表单错误
【发布时间】:2014-05-01 14:05:32
【问题描述】:

我在将表单提交到后端时遇到了麻烦。 当我点击提交时,它转到 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'),

我不知道我做错了什么

【问题讨论】:

    标签: jquery python ajax django


    【解决方案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)
    

    【讨论】:

    • 谢谢,但我的表单正在验证,只有数据没有保存。我没有从表单中获取数据以查看。正在创建空白对象。
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2016-06-22
    • 2020-11-24
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多