DJango upvoting, downvoting - functions in templates

169 views
Skip to first unread message

Jan Gregorczyk

unread,
Jun 2, 2020, 10:05:33 AM6/2/20
to Django users
I'm using this package https://pypi.org/project/django-vote/  and i want to do something smilar to this guy https://github.com/shellfly/django-vote/issues/51 - I need function callable in template, which will tell me if user has downvoted or upvoted, but exactly downvoted or exactly upvoted. I don't know where should I put it's code to make it work. It should take two arguments: user_id and answer.

My view:

def question(request, question_id):
question = get_object_or_404(Question, pk=question_id)
form = AnswerForm()
return render(request, 'questions/question.html', {'question': question, 'form' : form},)

I'd like to use this function here with upvote, downvote elements.:

{% for answer in question.answers.all %}
<h3>{{answer.author}}</h3>
<p>{{answer.content}}</p>
<p>
<i class="upvote" data-answer-id="{{answer.id}}">&#8593</i>
<span id="score-{{answer.id}}">{{answer.votes.count}}</span>
<i class="downvote" data-answer-id="{{answer.id}}">&#8595</i>
</p>
{% endfor %}

Also I'm not sure which functions can be called in Django templates. Only those having no arguments?
Reply all
Reply to author
Forward
0 new messages