對隨機圖像進行分級會導致tag.rand_img上的標題,url(etc)屬性消失,這意味着我無法顯示超過對象名稱的任何內容。從已標記庫中選擇一個隨機項目
view.py:
def locations_gallery(request):
queryset = Photo.objects.all()
tags = Tag.objects.usage_for_queryset(queryset, counts=True)
tags.sort(key=operator.attrgetter('count'), reverse=True)
for tag in tags:
image_tag = Tag.objects.get(name=tag)
tag.rand_img = TaggedItem.objects.get_union_by_model(Photo, image_tag).order_by('?')[:1]
ctxt['tags'] = tags
return render_to_response('locations_gallery.html', ctxt, RequestContext(request))
模板(簡化):
{% for tag in tags %}
{{ tag.name }}
<img src="{{ tag.rand_img.url }}">
{% endfor %}
如何將圖像添加爲額外的字段?這是我堅持的一點。 – 2012-01-31 10:51:17
我認爲只要做一個'tag.rand_img = img'就夠了。 – 2012-01-31 11:03:14
tag.rand_img =標記會導致對象失去標題,網址等所有屬性 – 2012-01-31 11:23:03