Django calling a function that returns a value doesn't work
I'm having a problem in calling a function that returns a result, from another function
To make it clear, my functions are:
def calculate_questions_vote(request):
useranswer = Answer.objects.filter (answer_by = request.user)
positive_votes = VoteUpAnswer.objects.filter(answer = useranswer)
negative_votes = VoteDownAnswer.objects.filter(answer = useranswer)
question_vote_rank = sum(positive_votes) - sum(negative_votes.count)
return question_vote_rank
def calculate_replies(request):
the_new = News.objects.filter(created_by = request.user)
reply = Reply.objects.filter(reply_to = the_new)
reply_rank = sum(reply)
return reply_rank
and I want to call them in another function, so that it could return a value. I'm calling the function form another function like this:
rank = calculate_questions_vote
Let's say I just want for now to display the value returned by the function calculate_questions_vote. Of course, I'm putting the rank variable in the context of the function.
My problem is that my output is:
<function calculate_questions_vote at 0x9420144>
How can I actually make it display the value returned by the function, instead of that st开发者_如何学JAVAring?
This is basic Python. What you are doing is simply referring to the function - assigning the function itself to another variable. To actually call a function, you need to use parenthesis after its name:
calculate_questions_vote()
In your case, you've defined that function as needing the request
object, so you need to use that in the call as well:
calculate_questions_vote(request)
you need to pass a request object to calculate_questions_vote
like:
rank = calculate_questions_vote(request)
精彩评论