Django - 使用模板标签和'with'? [英] Django - use template tag and 'with'?
本文介绍了Django - 使用模板标签和'with'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义模板标签:
I have a custom template tag:
def uploads_for_user(user):
uploads = Uploads.objects.filter(uploaded_by=user, problem_upload=False)
num_uploads = uploads.count()
return num_uploads
,我想做这样的事情,所以我可以正确复数:
and I'd like to do something like this, so I can pluralize properly:
{% with uploads_for_user leader as upload_count %}
{{ upload_count }} upload{{ upload_count|pluralize }}
{% endwith %}
但是, uploads_for_user leader
在此上下文中不起作用,因为'with'标签需要一个单一的值 - Django返回:
However, uploads_for_user leader
doesn't work in this context, because the 'with' tag expects a single value - Django returns:
TemplateSyntaxError at /upload/
u'with' expected format is 'value as name'
任何想法我可以如何解决这个问题?
Any idea how I can get round this?
推荐答案
您可以将其转换为过滤器:
You could turn it into a filter:
{% with user|uploads_for as upload_count %}
这篇关于Django - 使用模板标签和'with'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文