0

I am following Django By Example tutorial and unable to follow users. I click the follow button but nothing happens. I've went over that section over and over, copy and pasted the code. It still doesn't work.

Here is my 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 and 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 is the ajax in my 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 is the 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 for your help

rage mon
  • 53
  • 9
  • where's the view for the get request where you initialise data('id') and data('action')? When you set a breakpoint on your post view, what's the data you receive? And just to be sure, in your javascript console, you see a 'ko' status returned, confirming your view method is being called? – dirkgroten Jul 28 '17 at 11:21
  • @dirkgroten I do not really understand your question that well. Could you be a little more explicit – rage mon Jul 28 '17 at 11:26
  • Well, your script is posting `id: $(this).data('id')`, how is `$(this).data('id')` populated in the first place? Since $(this) is pointing to an `a.follow` element in your HTML page, I'm wondering how that is defined and which view is rendering that template. I'm just speculating here, that data('id') and data('action') are empty since in your `user_follow` method, that would return a status of `ko` (that's why I also asked if you actually see status `ko` being returned in your browser) – dirkgroten Jul 28 '17 at 11:31
  • ok the browser returns `{"status": "ok"}` in a similar function with a like button but the follow function gives out an error ` 500 (Internal Server Error)` – rage mon Jul 28 '17 at 11:38
  • 1
    well that means your django code is crashing somewhere, so you should be able to see the traceback code in your console. What's the traceback? – dirkgroten Jul 28 '17 at 12:57
  • The django code runs well but when I click the follow button it does nothing and but in the browser console it gives a 500 error. there is not traceback – rage mon Jul 28 '17 at 13:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150409/discussion-between-dirkgroten-and-rage-mon). – dirkgroten Jul 28 '17 at 13:10
  • I see the error in the views I was requesting to another instance of the model – rage mon Jul 28 '17 at 13:13

2 Answers2

0

The views should be this 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 and 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'})
rage mon
  • 53
  • 9
-1

You have misspelled your status value 'ok' to 'ko' Please change it to 'ok'

    except User.DoesNotExist:
            return JsonResponse({'status':'ok'})
    return JsonResponse({'status':'ok'})

I hope that was a simple mistake while checking in your js code. ?

Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36
  • I did that but does not solve the problem and I check in the tutorial book and it is 'ko' but not 'ok' – rage mon Jul 28 '17 at 11:22