0

Can someone help me with Django-ratings app? I'm trying to post a rating but the app doesn't seem to do anything. What part am I missing here? Really hard to find examples out there..

My function looks like this (jquery Raty plugin):

$('.raty').raty({
  click: function(score, evt) {
  var vote_url = "/rate/" + $(this).attr('value') + "/" + score + "/" ;

    $.ajax({
      url: vote_url,
      type: "GET",
      success: function(){
        alert('Vote successful!');
      }
});
   }
});

The GET seems to work but i can see in my admin that no votes/scores are registered.

In my URL:

url(r'rate/(?P<object_id>\d+)/(?P<score>\d+)/', AddRatingFromModel(), {
    'app_label': 'myapp',
    'model': 'MyModel',
    'field_name': 'rating',
}),

EDIT:

I'm getting a 404 error "Invalid model or app_label". But I'm pretty sure thoose are the correct ones.

user3199840
  • 525
  • 2
  • 13
  • 35
  • Could you show `AddRatingFromModel`? – sneawo Feb 12 '14 at 19:33
  • sneawo, It's just a view that is part of the original package. I don't think i should have to change something there. But i guess i could post it if you think it helps? Im trying to follow this: https://pypi.python.org/pypi/django-ratings – user3199840 Feb 12 '14 at 19:47
  • i think it's in here: https://github.com/dcramer/django-ratings – user3199840 Feb 12 '14 at 19:49
  • What does your ajax response returns? You can see this in Chrome Developer Tools > Network, etc. – Andrey Nelubin Feb 12 '14 at 21:04
  • Andrey, oh nice I didn't know about that. It says in status/text: 403 FORBIDDEN. Is that a CSRF related problem? I'm still quite new to all this. Not even sure if "post" is the method type i should use here.. – user3199840 Feb 12 '14 at 22:07

1 Answers1

1

This applications does not need the POST request. The easiest way to solve the problem is to set 'GET' method in ajax request

$.ajax({
    ...
    type: 'GET'
    ...

To avoid 404 you need to write model name in lowercase. In django.contrib.contenttypes app_label and model use lowercase.

url(r'rate/(?P<object_id>\d+)/(?P<score>\d+)/', AddRatingFromModel(), {
...
  'model': 'mymodel',
...
}),
Andrey Nelubin
  • 3,084
  • 1
  • 20
  • 25
  • Thanks! I've managed to get pass the 403-forbidden with post+csrf token and also by changing to GET (don't need csrf token on that right?). But instead of 403 i get the 404 returned now: "Invalid `model` or `app_label`." I cant understand why. I'm getting so tired with this. Do you have any ideas? – user3199840 Feb 13 '14 at 13:37
  • @user3199840 show your models.py of your app. Does folder contain `__init__.py`, have you added your `app` to `INSTALLED_APPS`? – Andrey Nelubin Feb 13 '14 at 16:11
  • Yes both "myapp" and the "djangoratings" app are listed in the settings "installed apps". "myapp" does have an __init__.py in it's folder. They both appear in the admin. And in my models i have a class named "MyModel" with a rating-field. I'm following this: https://pypi.python.org/pypi/django-ratings .I'll edit the post with my model. – user3199840 Feb 13 '14 at 16:36
  • OMG! I think it works!! Such a simple thing but i think i never would have found it on my own. Really cool of you taking your time to help me out! Thanks a lot man!!! – user3199840 Feb 13 '14 at 17:56