shellfly / django-vote

Simple vote for django
http://django-vote.readthedocs.io
Apache License 2.0
158 stars 49 forks source link

A basic template implementation and views function? #54

Closed tigrettin closed 6 years ago

tigrettin commented 7 years ago

I'm pretty new to Django and struggling to implement django-vote to my app.

For voting up a review in my Car page, I tried this in my template:

<form action="{% url 'reviews:review-vote-up' review.id %}" method="GET">
            <input type="submit" value="Up" name="vote-up" />
</form>

My URL:

url(r'^(?P<review_id>\d+)/up/$', views.review_vote_up, name="review-vote-up"),

My view function:

def review_vote_up(request, review_id):
    if request.GET.get("vote-up"):
        review = Review.objects.get(id=review_id)
        review.votes.up(request.user)

    context = {"make": review.car.make, 
    "model": review.car.model, 
    "platform": review.car.platform, 
    "body": review.car.body, 
    "version": review.car.version, 
    "years": review.car.years}
    return redirect("reviews:car", context)

But this doesn't register the vote, and it doesn't redirect me back to the Car page.

Could you give me a simple template implementation and view function example of voting up, or clarify my errors?

Any suggestion or feedback will be welcomed and greatly appreciated.

Thank you.

shellfly commented 7 years ago

You have to pass user_id instead of a user instance. We should raise an error here.

tigrettin commented 7 years ago

I changed that line as review.votes.up(request.user.id) but still can't register the vote. Is the rest OK?

shellfly commented 7 years ago

You can add some print to your view and check if the votes.up have been executed.

tigrettin commented 7 years ago

Yes, I've got {{ review.votes.count }} in my template and it shows "0", I can't execute upvoting through my view. But it works in the shell.