如何在Django模板中显示img文件? [英] How to display img file in django template?
问题描述
我知道标题有些误导,但我会解释所有内容.我正在django中创建商店,但是有一个小问题,我不知道该如何解决.好吧,对于每种产品,我想分配几张保存在数据库中的照片.这就是为什么我创建了 Photo
模型的原因,该模型通过 ForeignKey
连接到 Book
模型.
I know the title is a bit misleading but I explain everything. I am in the process of creating a store in django and there is a small problem that I do not know how to work around. Well, for each product I want to assign several photos that I save in the database. That's why I created the Photo
model, which is connected to the Book
model with a ForeignKey
.
整个问题是我不知道如何在商店的首页上指示分配给特定书籍的照片之一.当然,页面上有很多书是在 for循环
中生成的.
The whole problem is that I do not know how to indicate on the store's home page one of the photos that is assigned to a specific book. Of course, there are many books on the page that are generated in a for loop
.
如果有帮助,我将每本书的图片保存在一个单独的文件夹中,由功能 content_file_name
负责
If it is helpful, I save the pictures for each book in a separate folder, the function content_file_name
is responsible for that
当然,我愿意接受其他想法来实现如上所述的效果.我不确定我选择的路径是否最佳.
Of course, I am open to other ideas to achieve such an effect as described above. I am not sure if the path I have chosen is optimal.
#models.py
def content_file_name(instance, filename):
ext = filename.split('.')[-1]
filename = "%s.%s" % (instance.book.slug, ext)
return os.path.join('books_img', instance.book.slug, filename)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=300)
publisher = models.CharField(max_length=100)
price = models.FloatField()
slug = models.SlugField()
seller = models.ForeignKey(User, on_delete=models.CASCADE)
class Photo(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
file = models.ImageField(default=random_img, upload_to=content_file_name)
uploaded_at = models.DateTimeField(auto_now_add=True)
推荐答案
这实际上是一个关系问题,而不是编码问题.让我们深入研究
This is actually a relation problem more than a coding problem. Let's dive into it
这是您的 Book
类的简短版本,用于说明.
This is a short version of your Book
class for illustration.
class Book(models.Model):
pass
这是您的 Photo
类的简短版本,用于说明.
This is a short version of your Photo
class for illustration.
class Photo(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
file = models.ImageField(default=random_img, upload_to=content_file_name)
什么是 ForiegnKey
?- ForiegnKey
只是 ManyToOne
的一个奇特的名字,对吗?这里有多少个?
What's a ForiegnKey
?
- ForiegnKey
is just a fancy name for ManyToOne
right?, What's the many here and what's the one?
好吧,我们有一本书,里面有多张照片.因此,照片具有 Key
,就像门钥匙一样,它仅打开 这是门,这是 Key
.
Well, We have a book that has multiple photos. So the photo has a Key
, like a door key, It opens ONLY it's door, It's a Key
.
您的 Photo
类具有一个名为book的类变量.调用它就像 Photo.book
.但是,由于django也起作用,因此调用反向关系将是 book.photo_qs
,其中 photo_qs
是反向关系的默认名称.
Your Photo
class has a class variable called book. Calling it will be like Photo.book
. However, Because django acts too, Calling the reverse relation will be book.photo_qs
where photo_qs
is the default name for the reverse relations.
让我们改善它.
class Photo(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='photos')
file = models.ImageField(default=random_img, upload_to=content_file_name)
现在,和以前一样,它仍然是 photo.book
,但 book.photos
(来自相关名称)
Now, As before, It's still photo.book
but book.photos
(from the related name)
英语中,每个图书实例都有.photos(具有相关经理类型).因此,如果您有类似的书
In English, Every book instance has .photos (of related manager type). So if you get a book like
my_book = Book.objects.all()[0]
本书的.photos类型(相关经理"),使用起来就像
This book has .photos of type (Related Manager), Using it will be like
my_book.photos.all()
到这里都可以,现在的问题是,您希望将包含书本详细信息的图像发送到模板中,
It's ok till here, Now the problem is that you want to send the image with it's book details in a template,
您的视图是
def my_view(request):
my_book = Book.objects.all()[0] # first book
return render(request, 'my_template_path', context= {
'my_book': my_book,
})
现在您的模板可以访问 my_book
变量.
Now your template has access to my_book
variable.
在您的模板中
{% for photo in my_book.photos.all %}
<img src="{{ photo.file.url }}">
{% endfor %}
根据您的需要进行编辑.
Edit according to your needs.
这篇关于如何在Django模板中显示img文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!