2011-04-27 88 views
2

在我的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,但是我该如何去做这件事,然后如何将它显示在我的模板中?

宝宝说话。我是新来的。谢谢!

回答

1

那不是切片,你越来越指数10:

views.py:

latest_issues = Issue.objects.order_by('-date_added')[10] 

你会想:

latest_issues = Issue.objects.order_by('-date_added')[:10] 

要显示所有的一期封面图片:

{% for image in issue.images.all %} 
    {% if image.category == 'Cover' %} 
    <img src='{{ image.image.url }}' /> 
    {% endif %} 
{% endfor %} 
+0

它已经在我的观点中设置了这种方式。我会编辑它。 – AAA 2011-04-27 04:33:55

1

这样做的一种方法是在Issue类上创建一个属性来保存指定为“封面”的图像。这使您的模板尽可能简单。如果您只想显示一个封面,则可以为模型管理员添加一个干净的方法(或者您在发布模型中进行编辑的任何地方),以便仅允许将一个图像指定为“封面” ”。

#NOT TESTED 
#models.py 
from django.utils.functional import memoize 

class Issue(models.Model): 
    images = models.ManyToManyField(Image, blank=True, null=True) 

    @memoize 
    @property # @property means this is read-only 
    def cover(self): 
     return self.images.filter(category__exact='Cover')[:1] 

#views.py 
def index(request): 
    latest_issues = Issue.objects.order_by('-date_added').select_related(depth=1)[:10] 
    return render(request, 'comics/index.html', 
     {'latest_issues' : latest_issues}) 

#latest_issues.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.cover.url }}"> 
    </a> 
    <em>{{ issue.title }} #{{ issue.number }}</em> 
</li> 
{% endfor %} 

你可能会考虑使用整数持有的类别,而不是字符串的值,并且也把选择和选择的元组到一个单独的文件,这样就可以在多个地方更容易一些访问它们。我倾向于将这些放入“constants.py”文件中,尽管Python没有常量。

希望能帮助你。

+0

好的答案 - 一个改进是记忆'cover'属性,以便多次调用不会多次访问数据库。 – 2011-04-27 08:27:11

+0

谢谢丹尼尔。我完全忘记了内置的记忆:django.core.utils.functional.memoize - 显然我发布答案之前需要更多的咖啡。 – Brandon 2011-04-27 13:53:05

+0

Grr。 memoize位于:1.3中的django.utils.functional – Brandon 2011-04-27 14:00:00