2015-06-10 19 views
0

所以我的问题是,当我尝试查询图像URL,以便它可以发布到其相应的帖子所有已上传到媒体文件夹的图像正在呈现,即使在管理面板中,它显示每个帖子都有自己的图像,并且他们被分配到不同的帖子,相反,所有帖子都会被渲染到每个帖子。Django:查询ImageField所有图像正在呈现

我有的模型是SellPost这是用于创建一个帖子和SellPostImage是用于分配图像到帖子。

models.py

class SellPost(models.Model): 
    user = models.ForeignKey(User) 
    title = models.CharField(max_length=128) 
    category = models.ForeignKey(Category) 
    type = models.ForeignKey(SellPostType, default=None) 
    body = models.CharField(max_length=400) 
    price = models.DecimalField(decimal_places=1, max_digits=5, default=0.0) 
    views = models.IntegerField(default=0) 
    likes = models.IntegerField(default=0) 
    slug = models.SlugField(unique=True, default='automatic') 

    def save(self, *args, **kwargs): 
     self.slug = slugify(self.title) 
     super(SellPost, self).save(*args, **kwargs) 

    def __unicode__(self): 
     return self.title 




class SellPostImage(models.Model): 
    user = models.ForeignKey(User, null=True) 
    post = models.ForeignKey(SellPost) 
    pictures = models.ImageField(upload_to='post_images', blank=True) 

    def __str__(self): 
     return "{}".format(self.post) 

    class Meta: 
     verbose_name_plural = "Post Images" 

在视图中我试图创建一个上下文字典(因为我在Django新手,已经了解到,从探戈Django的,所以我用它去)的职位,然后图像:

views.py

def post(request, post_name_slug): 

    context_dict = {} 
    try: 
     post = SellPost.objects.get(slug=post_name_slug) 
     context_dict['post'] = post 

     post_image = SellPostImage.objects.all() 
     context_dict['post_image'] = post_image 

    except SellPost.DoesNotExist: 
     pass 

    return render(request, 'p.html', context_dict) 

,这里是我如何试图呈现他们在HTML文件。

p.html

<ul> 
     {% for post in posts %} 
     <li><a href="/p/{{ post.slug }}">{{ post.title }}</a> </li> 
     {% for post_images in post_image %} 

     <img style="width:200px; height:200px;" src="{{ post_images.pictures.url }}" /> 

    {% endfor %} 
     {% endfor %} 
    </ul> 
+1

你不应该在相应的帖子上过滤你的'SellPostImage'吗? 'post_image = SellPostImage.objects.filter(post = post)'。 – Evert

+0

有一件事我不明白:你在'context_dict'中传递了一个'post',但在你的模板中,你正在循环多个帖子:'{%for post in posts%}''。不知何故,您的视图和模板不兼容(或者您没有显示所有相关信息)。 – Evert

回答

1

你要过滤的SellPostImage的检索后:

post = SellPost.objects.get(slug=post_name_slug) 
context_dict['post'] = post 

post_image = SellPostImage.objects.filter(post=post) 
context_dict['post_image'] = post_image 

但是你可以很容易地直接把那个逻辑部分到您的模板:

{% for post in posts %} 
    <li><a href="/p/{{ post.slug }}">{{ post.title }}</a> </li> 
    {% for post_images in post.sellpostimage_set.all %} 
     <img style="width:200px; height:200px;" src="{{ post_images.pictures.url }}" /> 
    {% endfor %} 
{% endfor %} 

和那么你可以在你的意见中删除SellPostImage:

try: 
    post = SellPost.objects.get(slug=post_name_slug) 
    context_dict['post'] = post 
except SellPost.DoesNotExist: 
    pass 
+0

这会引发错误'Type Error at /'异常值:\t 'RelatedManager'对象不可迭代在'{%for post_images in post.sellpostimage_set%}' – qasimalbaqali

+1

对不起,答案是即时编写并带有未经测试的代码。查看更新的代码(提示:使用'.all')。 – Evert

+0

这样做谢谢你! – qasimalbaqali

1

在你post方法您查询所有SellPostImage S:

post_image = SellPostImage.objects.all() 

这就是为什么你得到每个岗位的所有图像。

可以过滤只有这样做与岗位相关的图像,而不是执行以下操作:

post_image = SellPostImage.objects.filter(post=post) 

它将提供全部图像进行特定的帖子。

+0

那么''看起来会是什么样子,因为我试过了你的答案,并使用相同的OP帖子,我仍然得到所有的图像。 – qasimalbaqali

+0

您实际上与您正在查看的帖子关联了多少张图片(在数据库中)? – OrenD

+0

对于每篇文章,我只有一个图像。在发布图片下的管理面板中,每个帖子只显示一张图片。 – qasimalbaqali