在我的index.html中,我試圖通過對添加的日期進行排序來獲取10個最新(漫畫書)問題,然後通過顯示這些問題的封面圖像像這樣的表:http://www.comicbookresources.com/previews在Django模板中通過Item.objects.all顯示圖像
Models.py:
class Image(models.Model):
CATEGORY_CHOICES = (
('Cover', 'Cover'),
('Scan', 'Scan'),
('Other', 'Other'),
)
title = models.CharField(max_length=256)
image = models.ImageField(upload_to="images/")
category = models.CharField(max_length=10, choices=CATEGORY_CHOICES)
contributor = models.ManyToManyField(Contributor, blank=True, null=True)
date_added = models.DateField(auto_now_add=True)
def __unicode__(self):
return self.title
class Meta:
ordering = ['title']
class Issue(models.Model):
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
....
date_added = models.DateTimeField(auto_now_add=True)
images = models.ManyToManyField(Image, related_name="images_inc", blank=True, null=True)
....
def __unicode__(self):
return u'%s #%s' % (self.title, self.number)
def get_absolute_url(self):
return "/issues/%s" % self.slug
class Meta:
ordering = ['-date_added']
Views.py
def index(request):
....
all_characters = Character.objects.all().filter(is_featured=True).order_by('name')
latest_issues = Issue.objects.order_by('-date_added')[:10]
....
t = loader.get_template('comics/index.html')
c = Context({
'all_characters': all_characters,
'latest_issues': latest_issues,
})
return HttpResponse(t.render(c))
現在,這裏是我的index.html文件是如何設置:
{% for issue in latest_issues %}
<li class="gallery">
<a href="{{ issue.get_absolute_url }}">
<img alt="{{ issue.title }} #{{ issue.number }}" title="{{ issue.title }} #{{ issue.number }}" src="{{ issue.images }}"></a>
<em>{{ issue.title }} #{{ issue.number }}</em>
</li>
{% endfor %}
{{ issue.images }}
顯示的那些ManyToManyDBManagex34982423事情之一,{{ issue.images.all }}
顯示類似「圖片:驚人的X戰警1蓋子A」,而{{ issue.images.url }
}顯示任何內容。
我需要它來顯示封面圖片和更多,具體來說,我需要它顯示一個圖像,歸類爲'封面',因爲有時會有變體封面,我不希望它顯示兩個一個問題的封面。我相信我必須修復我的views.py,但是我該如何去做這件事,然後如何將它顯示在我的模板中?
寶寶說話。我是新來的。謝謝!
它已經在我的觀點中設置了這種方式。我會編輯它。 – AAA 2011-04-27 04:33:55