我有一個自定義模板標籤:Django - 使用模板標籤和'with'?
def uploads_for_user(user):
uploads = Uploads.objects.filter(uploaded_by=user, problem_upload=False)
num_uploads = uploads.count()
return num_uploads
,我想要做這樣的事情,這樣我就可以適當地變複數:
{% with uploads_for_user leader as upload_count %}
{{ upload_count }} upload{{ upload_count|pluralize }}
{% endwith %}
然而,uploads_for_user leader
沒有在這方面的工作,因爲'with'標籤需要單個值 - Django返回:
TemplateSyntaxError at /upload/
u'with' expected format is 'value as name'
任何想法我怎麼能得到這個?
啊 - 我明白了。謝謝:) – AP257 2010-04-28 09:37:30
Django 1.5+可以讓你更簡潔地做到這一點。 {%with upload_count = user | uploads_for%} [django with](https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#with) – 2015-04-21 00:39:12