Answer the question
In order to leave comments, you need to log in
How to combine two models (Images and Products) in views?
how to connect two models (product and pictures) in views and display pictures in html accordingly? Yes, in the future it will be a separate form, but now you can find out for yourself.
Through the admin panel, I upload 3-4 photos for each project. And I don’t know how to bring one separately to the post about the project.
those. Now models has the following class:
class Portfolio (models.Model):
modeling='Modeling'
books ='Books'
engineering='Engineering'
category_ch = [
(modeling, 'Modeling'),
(books, 'Books'),
(engineering, 'Engineering'),
]
category = models.CharField('Category',max_length=100,choices=category_ch, default=modeling)
name = models.ForeignKey (Teams, related_name='maked', on_delete=models.PROTECT,blank=True)
short_discription = models.CharField("Name of the work", max_length=200, blank=True)
discription = models.TextField('Discription', blank=True)
сustomer = models.CharField('Customer', max_length=100, default='Заказчик')
created = models.DateTimeField('Date',auto_now_add=True)
class Meta:
verbose_name= 'Portfolio'
verbose_name_plural = 'Portfolios'
def __str__(self):
return self.short_discription
#def get_absolute_url(self):
#return reversed ('shop:product_detail', args=[self.category.slug, self.slug]')
class Image(models.Model):
image = models.ImageField('Picture of work', upload_to='products/%Y/%m/%d', blank=True)
product = models.ForeignKey(Portfolio, default=None, related_name='image', on_delete=models.PROTECT)
def portfolio_page(request):
portfolio = Portfolio.objects.all()
image_work= Image.objects.all()
ctx = {
'portfolio': portfolio,
'image':image_work,
}
return render(request, 'mainApp/portfolio_page.html',ctx)
{% for el in portfolio %}
<div class="portfolio_db">
<h3> {{ el.short_discription }} </h3>
{% for i in image %}
<img class="photo_work" src="{{ i.image_work }}" alt="Oh, there is something wrong here" width="155px" height="215px"></img>
{% endfor %}
<h4> Maked by {{ el.name }} </h4>
<h4> Ordered by {{ el.сustomer }} </h4>
</div>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question