2015-07-22 22 views
0

我需要實現AJAX並且其工作正常。但是,我在爲模型的每個實例應用ajax時遇到了問題。它只是應用在頂級對象上並在同一頁面上應用Ajax調用。對於剩餘的對象,當我點擊鏈接時,將其引導到新頁面並向我展示我不想要的JSON對象。如何對DJANGO中的所有模型對象應用ajax調用

Views.py

def upvote(request, post_id): 

if request.method == "POST": 
    p = get_object_or_404(Post, pk=post_id) 
    p.upvote += 1 
    p.save() 
    return JsonResponse({'count':p.upvote }) 

if request.method == "GET": 
    p = get_object_or_404(Post, pk=post_id) 
    p.upvote += 1 
    p.save() 
    data = { 
     'count' : p.upvote, 
    } 
    return JsonResponse({'count': p.upvote}) 

Urls.py

url(r'^(?P<post_id>[0-9]+)/upvote/$', views.upvote, name='upvote-detail'), 

Views.html

<script> 
    $(function(){ 
     $('#up_vote').click(function(e) { 
      e.preventDefault(); 
      window.alert("hello"); 
      console.log("hello"); 


      $.ajax({ 
      url: $(this).attr('href'), 
      type :'get' , 
      success : function (data){ 
       // alert('success'); 
       $('#upvote_count').html(data.count); 
      }, 
      failure : function (data){ 
       alert('failure') ; 
      } 
      }) ; // ajax call 

     }); // upvote link call 
</script> 

<div id="columns"> 
    {% for post in object_list %} 

     <div class="pin"> 
      <a href="/posts/{{ post.id }}/"> 

      <img src= "/static/media/{{post.image}}" /> 
      </a> 
      <p> 
       {{post.description}} 
       (<a href="{% url "post-edit" pk=post.id %}">edit</a>) 
      </p> 


      <div style="float:left"> 
       <a href='{% url 'upvote-detail' post.id %}' id='up_vote'>Up vote </a> 
       <span id='upvote_count'> {{ post.upvote }} </span> 
      </div> 


      <div style="float:right"> 
       <a href='{% url 'downvote-detail' post.id %}' id='down_vote'> Down vote </a> 
       <span id='downvote_count'>{{post.downvote}}</span> 
      </div> 


     </div> 
    {% endfor %} 

    </div> 

這裏是我的所有文件。目前AJAX調用工作得很好。它只是應用在LOOP對象的頂部(第一個)。對於剩餘對象,當我點擊鏈接時,它會將我顯示到新頁面。有人可以找出問題嗎? 謝謝,

+1

基本上返回'JsonResponse',一個完整的答案關於如何實現ajax對於堆棧溢出來說過於寬泛,它已經在文檔中顯示過 – Sayse

+0

@Sayse你能告訴我在哪裏可以找到文檔嗎?或任何有用的鏈接 – user3487775

+1

我花了大約15秒來找到[上的答案](http://stackoverflow.com/a/20307569/5098707)。請在詢問之前做一些調查。 –

回答

0

試試這個:

def upvote(request, post_id): 
    p = get_object_or_404(Post, pk=post_id) 
    errors = [] 
    data = {} 
    try: 
     p.upvote += 1 
     p.save() 
     data = { 
      'success': 1, 
     } 
    except: 
     errors.append(['There was an error']) 
     data = { 
      'success': 0, 
      'errors':errors, 
     } 
    return HttpResponse(json.dumps(data)) 
1
在views.py

def detail(request): 
    return render(request, 'detail.html', {'posts': Post.objects.all()}) 

def upvote(request, post_id): 
    if request.method == "POST": 
     p = get_object_or_404(Post, pk=post_id) 
     p.upvote += 1 
     p.save() 
     return JsonResponse({'count':p.upvote }) 
    return JsonResponse({}) 

在detail.html:

{% for post in posts %} 
    <a href='{% url 'upvote-detail' post.id %}' id='up_voite'>Up voite <span id='voite_count'> {{ post.upvote }} </span> </a> 
{% endfor %} 

<script> 
    $(function(){ 
     $('#up_voite').click(function(e) { 
      e.preventDefault(); 
      $.post($(this).attr('href'), function(data) { 
       $(this).find('span').html(data.upvote); 
      }); 
     }); 
    }); 
</script> 

代碼不檢查

+2

這很好,除了你真的不應該做一個數據庫修改操作得到。不妨讓JS做一個POST並檢查upvote函數。 –

+0

你是對的,更新回答 – comalex3

+0

@ comalex3謝謝。由於某種原因,它不更新upvote數據。相反{{post.upvote}}保持不變。 標記確實顯示了鏈接,但它沒有更新upvote並且沒有顯示span標籤。 – user3487775

相關問題