2017-12-29 3 views
0

Das ist mein Template-Fragment für Kommentare/Wie URL an Ajax Erfolg übergeben?

{% for comment in comments %} 
    <div class="comment-wrapper"> 
     <a href="{% url 'comment:update_comment' slug=product.slug id=comment.id %}" class="comment-edit-img"> 
      <img src="{% static 'media/work_images/icons-edit.png' %}" alt=""> 
     </a> 
     <a href="{% url 'comment:delete_comment' comment.pk %}" class="comment-del-img"> 
      <img src="{% static 'media/work_images/icons-delete.png' %}" alt=""> 
     </a> 
     <p class="comment-text">{{comment.comment_text}}</p> 
     {% if comment.update_date != comment.posted_date %} 
      <span class="comment-author"> 
       {% trans 'Posted' %}: 
       {{comment.author}} 
      </span> 
      <span class="comment-date"> 
       {% blocktrans trimmed with update=comment.update_date %} 
        {{update}} 
       {% endblocktrans %} 
      </span> 
     {% else %} 
      <span class="comment-author"> 
       {% trans 'Updated by' %}: 
       {{comment.author}} 
      </span> 
      <span class="comment-date"> 
       {% blocktrans trimmed with posted=comment.posted_date %} 
        {{posted}} 
       {% endblocktrans %} 
      </span> 
     {% endif %} 
    </div> 
    {% endfor %}enter code here 

Mein Ajax-Code/

$('#add-comment').click(function (e) { 
    e.preventDefault(); 
    var form = $('#add-comment-form'); 
    $.post(form.attr('action'), form.serialize()).done(function(data) { 
     $('#comment-box').prepend(
      "<div class='comment-wrapper'>"+ 
      "<a href='???' class='comment-edit-img'>up</a>"+ 
      "<a href='???' class='comment-del-img'>x</a>"+ 
      "<p class='comment-text'>"+data.text+"</p>"+ 
      "<span class='comment-author'>"+data.author+"</span>"+ 
      "<span class='comment-date'>"+data.date+"</span>"+ 
      "</div>" 
     ); 
    }) 
}); 

Wie kann ich die URL der Adresse für Tag <a> die gleichen wie in der Vorlage übergeben? Oder gibt es eine bessere Lösung für Ajax?

+0

Bitte versuchen Sie, Titel nützlich und prägnant zu halten, und frei von gesprächigen bitte-Half-Me-Material so viel wie möglich. Es kann sich lohnen, die Titelseite zu durchsuchen, um zu sehen, wie Titel allgemein geschrieben werden. – halfer

Antwort

0

Eine gängige Praxis besteht darin, die URLs JS-Variablen in Ihrer Vorlagendatei zuzuordnen und sie in der JS-Datei zu verwenden.

<script> 
var URLS = { 
    addItem:  '{% url "cart:cart-add" %}', 
    removeItem:  '{% url "cart:cart-remove" %}', 
} 
</script> 

Und dann mit ihnen URLS.addItem

+0

Danke, aber wie gebe ich eine Variable an das Tag selbst, so dass die Verbindung funktioniert? –

+0

String-Konkatation. Wie du bist du mit 'data.text'. '" up "' – mking

+0

Danke, bereits verstanden, aber mit Variablen, die es nicht ausging, aus der Sicht als Daten weitergegeben. –

0

Dank. Aufgabe ist gelöst. Hier ist der Code, plötzlich jemand hilft:

$('#add-comment').click(function (e) { 
    e.preventDefault(); 
    var form = $('#add-comment-form'); 
    $.post(form.attr('action'), form.serialize()).done(function(data) { 
     $('#comment-box').append(
      "<div class='comment-wrapper'>"+ 
      "<a class='comment-edit-img'"+"href='"+data.update_url+"'>"+"<img src='/static/media/work_images/icons-edit.png'>"+"</a>"+ 
      "<a href='#' id="+data.id+" class='comment-del-img' data-toggle='modal' data-target='#category-del-modal'>"+ 
      "<img src='/static/media/work_images/icons-delete.png'>"+ 
      "</a>"+ 
      "<p class='comment-text'>"+data.text+"</p>"+ 
      "<span class='comment-author'>"+data.author+"</span>"+ 
      "<span class='comment-date'>"+data.date+"</span>"+ 
      "</div>" 
     ); 
    }) 
});