简体   繁体   中英

Django Like button ajax

I'm been trying to create like button with ajax. In my model strain i have a field call user_like = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='strains_liked', blank=True)

This is my views.py

@login_required
@require_POST
def strain_like(request):
    strain_id = request.POST.get('id')
    action = request.POST.get('action')
    if strain_id and action:
        try:
            strain = Strain.objects.get(id=strain_id)
            if action == 'like':
                strain.user_like.add(request.user)
            else:
                strain.user_like.remove(request.user)
            return JsonResponse({'status': 'ok'})
        except:
            pass
    return JsonResponse({'status': 'ok'})

this is urls.py

url(r'^like/$', views.strain_like, name='like')

and this is my templates

{% block content %}
{{ strain.name }}

    {% with total_likes=strain.user_like.count user_like=strain.user_like.all %}
       <span class="count">
           <span class="total">{{ total_likes }}</span> like
       </span>
        <a href="#" data-id="{{ strain.id }}" data-action="{% if request.user in user_like %}un{% endif %}like" class="like">
        {% if request.user not in user_like %}
            Like
        {% else %}
            Unlike
        {% endif %}
        </a>
    {% endwith %}
{% endblock %}
<script>
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});
    $(document).ready(function () {
$('a.like').click(function(e){
            e.preventDefault();
            $.post('{% url "strains:like" %}',
             {
                 id: $(this).data('id'),
                 action: $(this).data('action')
             },
             function(data){
                if (data['status'] == 'ok')
                {
                    var previous_action = $('a.like').data('action');
                    $('a.like').data('action', previous_action == 'like' ? 'unlike' : 'like');
                    $('a.like').text(previous_action == 'like' ? 'unlike' : 'like');

                    var previous_likes = parseInt($('span.count .total').text());
                    $('span.count .total').text(previous_action == 'like' ? previous_likes + 1 : previous_likes - 1);
                }
            }
            );
     });
    })
</script>

when I click to like or unlike I have this error in developer tools

IMG with error

Your url for this action is r'^like/$'. But you send a request for /strains/like, that is not matched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM