如何将url传递给ajax成功?

时间:2017-12-29 13:59:36

标签: jquery ajax django

这是我的评论模板片段

{% 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

我的ajax代码/

$('#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>"
        );
    })
});

如何传递标签<a>的地址网址与模板中的网址相同?或者也许对ajax有更正确的解决方案?

2 个答案:

答案 0 :(得分:1)

通常的做法是将url分配给模板文件中的JS变量,并在JS文件中使用它们。

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

然后使用它们URLS.addItem

答案 1 :(得分:0)

感谢。任务解决了。这是代码,突然有人会帮忙:

$('#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>"
        );
    })
});