如何在 Django 中渲染单个视图时将一个模型中的字段用于另一个模型
How to use field from one model into other model while rendering a single view in Django
这是我的模型
class Gallery(models.Model):
title = models.CharField(max_length=30)
description = models.TextField(blank=True)
featured_image = models.ImageField(upload_to="gallery")
def __str__(self):
return u'{}'.format(self.title)
class GalleryImage(models.Model):
title = models.CharField(max_length=50, blank=True)
description = models.TextField(blank=True)
gallery = models.ForeignKey(Gallery)
image = models.ImageField(upload_to="gallery")
def __str__(self):
return u'{}'.format(self.title)
这是我的观点:
def gallery(request):
galleries = Gallery.objects.all()
return render(request, "gallery/home.html", {'galleries': galleries})
def gallery_section(request, gallery_id):
gallery_images = GalleryImage.objects.filter(gallery_id=gallery_id)
return render(request, "gallery/section.html",{'gimages': gallery_images})
我想从图库模型调用 featured_image
字段并在 section.html 渲染文件中使用它。
无法得到这个为什么我不能访问这个字段。
模板代码在section.html:
.about{
background-image: url({{ gallery.featured_image.url }});
background-size: cover;
}
home.html中的模板代码:
{% for gallery in galleries %}
<a href="gallery/{{ gallery.id }}">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 people-desc text-center">
<img src="{{ gallery.featured_image.url }}" alt="" class="img-responsive people">
<h4>{{ gallery.title }}</h4>
</div>
</a>
{% endfor %}
当我点击一个标签时,它会将我带到 section.html 页面,但我也想在该页面上使用 featured_image
。
如果您想在视图 gallery_section
中使用 Gallery
对象,您必须将该对象传递到您的模板中:
def gallery_section(request, gallery_id):
gallery = Gallery.objects.get(id=gallery_id)
gallery_images = GalleryImage.objects.filter(gallery_id=gallery_id)
return render(request, "gallery/section.html",{'gimages': gallery_images, 'gallery': gallery})
它不会自动或神奇地出现在您的模板中。如果你在循环图像时使用它,你也可以这样做:
{% for image in gimages %}
{% image.gallery.featured_image.url %}
{% endfor %}
但要在该循环之外使用,您必须将图像传递给模板才能获取它。
这是我的模型
class Gallery(models.Model):
title = models.CharField(max_length=30)
description = models.TextField(blank=True)
featured_image = models.ImageField(upload_to="gallery")
def __str__(self):
return u'{}'.format(self.title)
class GalleryImage(models.Model):
title = models.CharField(max_length=50, blank=True)
description = models.TextField(blank=True)
gallery = models.ForeignKey(Gallery)
image = models.ImageField(upload_to="gallery")
def __str__(self):
return u'{}'.format(self.title)
这是我的观点:
def gallery(request):
galleries = Gallery.objects.all()
return render(request, "gallery/home.html", {'galleries': galleries})
def gallery_section(request, gallery_id):
gallery_images = GalleryImage.objects.filter(gallery_id=gallery_id)
return render(request, "gallery/section.html",{'gimages': gallery_images})
我想从图库模型调用 featured_image
字段并在 section.html 渲染文件中使用它。
无法得到这个为什么我不能访问这个字段。
模板代码在section.html:
.about{
background-image: url({{ gallery.featured_image.url }});
background-size: cover;
}
home.html中的模板代码:
{% for gallery in galleries %}
<a href="gallery/{{ gallery.id }}">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 people-desc text-center">
<img src="{{ gallery.featured_image.url }}" alt="" class="img-responsive people">
<h4>{{ gallery.title }}</h4>
</div>
</a>
{% endfor %}
当我点击一个标签时,它会将我带到 section.html 页面,但我也想在该页面上使用 featured_image
。
如果您想在视图 gallery_section
中使用 Gallery
对象,您必须将该对象传递到您的模板中:
def gallery_section(request, gallery_id):
gallery = Gallery.objects.get(id=gallery_id)
gallery_images = GalleryImage.objects.filter(gallery_id=gallery_id)
return render(request, "gallery/section.html",{'gimages': gallery_images, 'gallery': gallery})
它不会自动或神奇地出现在您的模板中。如果你在循环图像时使用它,你也可以这样做:
{% for image in gimages %}
{% image.gallery.featured_image.url %}
{% endfor %}
但要在该循环之外使用,您必须将图像传递给模板才能获取它。