Python Django-ListView-列出分配给帖子的图像
您好,我想在我的博客应用程序中列出所有添加到我文章中的图像 Models.pyPython Django-ListView-列出分配给帖子的图像,python,django,listview,Python,Django,Listview,您好,我想在我的博客应用程序中列出所有添加到我文章中的图像 Models.py class Post(models.Model): title = models.CharField(max_length=100) content = RichTextField(blank=True, null=True) class PostImage(models.Model): post = models.ForeignKey(Post, default=None, on_d
class Post(models.Model):
title = models.CharField(max_length=100)
content = RichTextField(blank=True, null=True)
class PostImage(models.Model):
post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE, related_name='postimages')
image = models.ImageField(upload_to='gallery/')
我想我应该重写get_queryset方法,但不知道如何重写。有什么想法吗?谢谢
编辑:
Views.py
class PostGalleryView(ListView):
model = PostImage
template_name = 'blog/gallery.html'
context_object_name = 'images'
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
paginate_by = 10
def get_queryset(self):
public_posts = Post.objects.all()
return public_posts
请提供您的视图。@binpy views.py将loop添加到您的上下文对象
图像中。每个迭代器(x)都有属性x.image
,它是图像的相对路径。然后显示图像
。顺便说一句,别忘了在django模板文件中{%load static%}
。