django在模板中调用无请求视图函数
我正在制作一些视图函数来计算社区中一个用户的排名。 我的问题是我想在其个人资料中显示传入每个用户的排名,但我不知道如何,因为我没有请求和 render_to_response (因为我猜它们不需要) 我的代码:
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
def calculate_votes(request):
the_new = News.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = the_new)
vote_rank = sum(vote)
return vote_rank
def personal_rank(request):
personal_rank = question_vote_rank + reply_rank + vote_rank
return personal_rank
在 UserProfiles 中:
user = request.user
personal_rank = calculate_questions_vote(user) + calculate_replies(user) + personal_rank(user)
但我的错误是:
错误绑定参数 0 - 可能不受支持的类型。
mt方法正确吗?我应该如何调用 profile_view def 中的排名函数?
谢谢!
I am making some view functions to calculate the rank of one user in a community.
My problem is that i want to display the rank afferent to each user, in its profile, and i don't know how, since i don;t have a request and a render_to_response (cause i guessed they are not needed)
my code:
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
def calculate_votes(request):
the_new = News.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = the_new)
vote_rank = sum(vote)
return vote_rank
def personal_rank(request):
personal_rank = question_vote_rank + reply_rank + vote_rank
return personal_rank
and in UserProfiles:
user = request.user
personal_rank = calculate_questions_vote(user) + calculate_replies(user) + personal_rank(user)
but my error is:
Error binding parameter 0 - probably unsupported type.
Is mt approach correct? How should i call the rank function in the profile_view def ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在视图中调用该函数,例如
rank = individual_rank(reuest.user)
并将rank
添加到您的上下文中。我不会将此函数称为“视图”函数,因为它们不处理请求,也不返回 HttoResponce;它们是更多的“帮助”功能,如果它们处理单独的实体(例如新闻和投票),那么它们也不属于模型。他们的下降地点是例如。
utils.py
。您可以从views.py
中导入它们,并以用户作为参数调用它们(如果是实际用户,则为request.user
)。这是有道理的,你不能从任何地方访问
request
,因为这迫使你更多地保留类似 mvc 的设计,如果你需要在某个地方请求,你需要从原始视图函数传递它!您应该将最后一个函数更改为:
如果您有类似的东西,您还可以将最后一个函数添加到您的 User 或 UserProfile 模型类中,然后调用例如。
my_user.personal_rank()
或my_user.get_profile().personal_rank()
。YOu can call the function in your view like
rank = personal_rank(reuest.user)
and addrank
to your context then.I wouldnt call this functions "view"-functions, since they are not dealing with a request nor are they returning a HttoResponce; they are more "helper" functions and also don't belong to a model if they are dealing with seperate entities (eg. News & Vote). A descent place for them would be eg.
utils.py
. You import them from there in yourviews.py
and call them with the user as parameter (if it's the actual user it'srequest.user
).It makes sense that you can't access
request
from everywhere, because this forces you to keep more to a mvc-like design, if you need request somewhere you need to pass it on from your original view function!You should change your last function to:
You could also add this last function to your User or UserProfile model class if you have something like that and then call eg.
my_user.personal_rank()
ormy_user.get_profile().personal_rank()
.lazerscience对你的疑问给出了很好的解释。我只是想指出一些可能的性能问题。
请注意,为每个用户调用此方法会产生 7 个查询。如果您的案例要显示所有用户的排名,则可能需要花费大量时间。我建议进行一些缓存(就像在 StackOverflow 中一样)。这里有很多选项,例如,您可以将特殊的 rank 字段分配给
User
模型(或UserProfile
如果User
> 是auth.User
),您可以在其中存储预先计算的排名(在某些操作之后计算,例如新闻保存或在某些计划的时间间隔内)。lazerscience gave good explanation for your guestion. I just want to point some possible performance issues.
Please note that calling this method for each User yields with 7 queries. If your case will be showing rank for all users it might take a lot of time. I suggest make some caching (like in StackOverflow). There are many options here, for example you can assign special rank field to a
User
model (orUserProfile
ifUser
isauth.User
) in which you could store precalaculated rank (calculated just after some operation, like News saving or in some scheduled intervals).