0

我想用 ajax 在 django 中发布一些文本,并保存输入数据,在同一页面中显示数据并且不刷新,比如 twitter。

我的js:

$('#SumbitButton').click(function(){
        var data_1 = $('#data_1').val()
        var data_2 = $('#data_2').val()
        var data_3 = $('#data_3').val()
        var data_4 = $('#data_4').val()
        var user = $('#AuthorName').text()
        var authorprotrait = $('#UserProprait').html()
    if(data_1.length>0){
        $.ajax({
            type: 'POST',
            url: '/post/',
            data:{'data_1':data_1,'data_2':data_2,'data_3':data_3,'data_4':data_4}, 
            async: false,
            error: function(msg){alert('Fail')},
            success:  function(msg){
                $('#TopicWrap').prepend("<div class='topic-all even'><div class='topic-left'><div class='topic-author'><div class='topic-author-protrait'>"+authorprotrait+"</div><div class='topic-author-nickname'>"+authorname+"</div></div></div><div class='topic-right'><div class='topic-meta'><span class='topic-datetime'></span></div><div class='topic-content-wrap'><div class='topic-content'>"+msg+"</div></div></div><div class='clearfix'></div></div>");
                $('#data_1').val('');   
                $('#data_2').val('');
                $('#data_3').val('');
                $('#data_4').val('');   
            }
        });
    } else {
        alert('The data_1's length  error !'+data_1.length);  
    }   
});

和html:

<div id="TopicTextarea">
    <div>
        data1:<input tabindex="4" id="data_1"  type="text" name="data1"  value="" maxlength="6"  placeholder=""/></br>
        data2:<input tabindex="4" id="data_2"  type="text" name="data2"  value="" maxlength="8"  placeholder=""/></br>
        data3:<input tabindex="4" id="data_3"  type="text" name="data3"  value="" maxlength="10"  placeholder=""/></br>
        data4:<input tabindex="4" id="data_4"  type="text" name="data4"  value="" maxlength="10"  placeholder=""/>  
        <div id="TopicFormControl">
        <button type="button" id="SumbitButton" class="button orange">sumbit</button>
        </div>
    </div>
   <div class="clearfix"></div>
</div>
<div id="TopicWrap">
{% include 'topics.html'%}
</div>

和意见:

@login_required
def post(request):

    assert(request.method=='POST' and request.is_ajax()==True)
    data_1 = smart_unicode(request.POST['data_1'])
    data_2 = smart_unicode(request.POST['data_2'])
    data_3 = smart_unicode(request.POST['data_3'])
    data_4 = smart_unicode(request.POST['data_4'])

    data_obj = DataPool(user=request.user,data_1=data_1,data_2=data_2,data_3=data_3, data_4=data_4)
    data_obj.save()

    content = '"+data_3+"  "+data_4+" "+data_1+"("+data_2+")' 

    response = HttpResponse(cgi.escape(content))

    return response

当我输入数据并单击 sumbit 按钮时,它可以保存数据,但不能显示数据。我的代码有什么问题?

谢谢。

4

1 回答 1

1

首先,使用诸如海报之类的东西来测试您的视图是否返回了您所期望的。只有这样您才应该开始构建 AJAX 部分。

至于出了什么问题……很难说,因为您只是写它“无法显示数据”而不是说它实际上在做什么。我怀疑这与您认为的这条线有关:

content = '"+data_3+"  "+data_4+" "+data_1+"("+data_2+")' 

我不太清楚你认为这会做什么,但它实际上做了什么:

>>> '"+data_3+"  "+data_4+" "+data_1+"("+data_2+")'
'"+data_3+" \xe3\x80\x80"+data_4+"\xe3\x80\x80"+data_1+"("+data_2+")'

也就是说,它生成一个包含子字符串"data_3"(等)的字符串。它包括提交的数据。

于 2012-05-13T07:05:14.700 回答