1

I'm having trouble registering a vote with registering a vote with django-voting: https://github.com/brosner/django-voting

I'm trying to technically upvote a comment object. Thus adding to it's score.

This is what I have so far:

Template:

<form method="POST" action="/comments/{{ comment.id }}/up/vote/">
    {% csrf_token %}
    <button type="submit">Thumbs up!</button>
</form>

urls

widget_dict = {
    'model': Comment,
    'template_object_name': 'comment',
    'allow_xmlhttprequest': True,
    }


 #users can comment on event objects, And VOTE on comments
 urlpatterns = patterns('',
    url(r'^$', 'event.views.index'),
    url(r'^(?P<id>\d+)/$', 'event.views.detail'),
    url(r'^comments/(?P<object_id>\d+)/(?P<direction>up|down|clear)/vote/?$', vote_on_object, widget_dict),
)

With this I get directed to a 404.

The docs give an example of :

from django.conf.urls.defaults import *
from voting.views import vote_on_object
from shop.apps.products.models import Widget

widget_dict = {
    'model': Widget,
    'template_object_name': 'widget',
    'allow_xmlhttprequest': True,
}

urlpatterns = patterns('',
    (r'^widgets/(?P<object_id>\d+)/(?P<direction>up|down|clear)vote/?$', vote_on_object, widget_dict),
)

Also, I can't add votes through the admin? Unable to add votes

I have no idea what widget_dict actually is. I'm just trying to have the form post to vote_on_object. Has anyone successfully posted a vote? If so, what am I doing wrong? Thanks for your help in advance.

Modelesq
  • 5,192
  • 20
  • 61
  • 88

1 Answers1

1

I think you have a typo and you're forgetting a '/'

your url in the form should be '/widgets/{{ comment.id }}/up/vote/' or the pattern should start with comments

And then the pattern, you forget a slash:

(?P<direction>up|down|clear)vote/?$

should be

(?P<direction>up|down|clear)/vote/?$

If you want to check routing, you can do from the shell

import re
re.match(r'^widgets/(?P<object_id>\d+)/(?P<direction>up|down|clear)/vote/?$', '/comment/1/up/vote/')

and see it works

Benoir
  • 1,244
  • 10
  • 10
  • Changed my post to your suggestions. Thanks for pointing it out, However, it did not solve my problem. I'm still getting a 404 when I try to post a vote. – Modelesq Jul 27 '12 at 20:16
  • Try doing it from the shell and make sure your regex's work -- I cleaned up my response (the formatting was off) to show you how to test the url routes from the shell – Benoir Jul 27 '12 at 23:15