如何在 django 模板中显示 img 文件? [英] How to display img file in django template?
问题描述
我知道标题有点误导,但我解释了一切.我正在 django 中创建商店,但有一个我不知道如何解决的小问题.好吧,对于每个产品,我想分配几张我保存在数据库中的照片.这就是我创建 Photo
模型的原因,该模型通过 ForeignKey
连接到 Book
模型.
整个问题是我不知道如何在商店的主页上指明分配给特定书籍的其中一张照片.当然,页面上有很多书籍是在for循环
中生成的.
如果有帮助,我将每本书的图片保存在一个单独的文件夹中,函数 content_file_name
负责
当然,我对实现上述效果的其他想法持开放态度.我不确定我选择的路径是否最优.
#models.pydef content_file_name(实例,文件名):ext = 文件名.split('.')[-1]文件名 = "%s.%s" % (instance.book.slug, ext)return os.path.join('books_img', instance.book.slug, 文件名)类书(模型.模型):标题=models.CharField(max_length=100)作者 = models.CharField(max_length=300)出版商 = models.CharField(max_length=100)价格 = 模型.FloatField()弹头=models.SlugField()卖家 = models.ForeignKey(用户,on_delete=models.CASCADE)类照片(模型.模型):book = models.ForeignKey(Book, on_delete=models.CASCADE)file = models.ImageField(默认=random_img,upload_to=content_file_name)Uploaded_at = models.DateTimeField(auto_now_add=True)
这实际上是一个关系问题,而不是编码问题.让我们深入了解一下
这是用于说明的 Book
类的简短版本.
class Book(models.Model):经过
这是用于说明的 Photo
类的简短版本.
class Photo(models.Model):book = models.ForeignKey(Book, on_delete=models.CASCADE)file = models.ImageField(默认=random_img,upload_to=content_file_name)
什么是 ForiegnKey
?- ForiegnKey
只是 ManyToOne
的一个奇特的名字吧?
好吧,我们有一本书有多张照片.所以这张照片有一个Key
,就像一把门钥匙,它ONLY是它的门,它是一个Key
.
您的 Photo
类有一个名为 book 的类变量.调用它就像 Photo.book
.但是,由于 django 也起作用,所以调用反向关系将是 book.photo_qs
其中 photo_qs
是反向关系的默认名称.
让我们改进它.
class Photo(models.Model):book = models.ForeignKey(Book, on_delete=models.CASCADE,related_name='photos')file = models.ImageField(默认=random_img,upload_to=content_file_name)
现在,和以前一样,它仍然是 photo.book
但book.photos
(来自相关名称)
在英语中,每个书实例都有 .photos(相关经理类型).所以如果你得到一本书像
my_book = Book.objects.all()[0]
这本书有.photos类型(相关经理),使用它就像
my_book.photos.all()
到这里都没问题,现在的问题是您想在模板中发送带有书籍详细信息的图像,
你的观点是
def my_view(request):my_book = Book.objects.all()[0] # 第一本书返回渲染(请求,'my_template_path',上下文= {'我的书':我的书,})
现在您的模板可以访问 my_book
变量.
在您的模板中
{% for photo in my_book.photos.all %}<img src="{{ photo.file.url }}">{% 结束为 %}
根据您的需要进行编辑.
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
.
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
.
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
This is a short version of your Book
class for illustration.
class Book(models.Model):
pass
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)
What's a ForiegnKey
?
- ForiegnKey
is just a fancy name for ManyToOne
right?, What's the many here and what's the one?
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
.
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.
Let's improve it.
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)
Now, As before, It's still photo.book
but book.photos
(from the related name)
In English, Every book instance has .photos (of related manager type). So if you get a book like
my_book = Book.objects.all()[0]
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,
Your view is
def my_view(request):
my_book = Book.objects.all()[0] # first book
return render(request, 'my_template_path', context= {
'my_book': my_book,
})
Now your template has access to my_book
variable.
in your template
{% for photo in my_book.photos.all %}
<img src="{{ photo.file.url }}">
{% endfor %}
Edit according to your needs.
这篇关于如何在 django 模板中显示 img 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!