jquery - Building an AJAX view to follow users -
i following django example tutorial , unable follow users. click follow button nothing happens. i've went on section on , over, copy , pasted code. still doesn't work.
here views
@ajax_required @require_post @login_required def user_follow(request): user_id = require.post.get('id') action = request.post.get('action') if user_id , action: try: user = user.objects.get(id=user_id) if action == 'follow': contact.objects.get_or_create(user_form=request.user,user_to=user) else: contact.objects.filter(user_form=request.user,user_to=user).delete() return jsonresponse({'status':'ok'}) except user.doesnotexist: return jsonresponse({'status':'ko'}) return jsonresponse({'status':'ko'})
this ajax in html block
{% block domready %} $('a.follow').click(function(e){ e.preventdefault(); $.post('{% url "user_follow" %}', { id: $(this).data('id'), action: $(this).data('action') }, function(data){ if (data['status'] == 'ok') { var previous_action = $('a.follow').data('action'); // toggle data-action $('a.follow').data('action', previous_action == 'follow' ? 'unfollow' : 'follow'); // update total followers var previous_followers = parseint( $('span.count .total').text()); $('span.count .total').text(previous_action == 'follow' ? previous_followers + 1 : previous_followers - 1); } } ); }); {% endblock %}
this url
**
#users url(r'^user/$', views.user_list, name = 'user_list'), url(r'^users/follow/$', views.user_follow, name='user_follow'), url(r'^users/(?p<username>[-\w]+)/$', views.user_detail, name = 'user_detail'),
**
thanks help
the views should instead
@ajax_required @require_post @login_required def user_follow(request): user_id = request.post.get('id') action = request.post.get('action') if user_id , action: try: user = user.objects.get(id=user_id) if action == 'follow': contact.objects.get_or_create(user_from=request.user,user_to=user) else: contact.objects.filter(user_form=request.user,user_to=user).delete() return jsonresponse({'status':'ok'}) except user.doesnotexist: return jsonresponse({'status':'ko'}) return jsonresponse({'status':'ko'})
Comments
Post a Comment