pythondjangopython-3.xdjango-modelsdjango-related-manager

Django Display first item from related_name list


I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented two models ItemModel and Images so every item will have multiple images and added item into the images model and add a related_name but when I try to access these images in Django template it's not providing any information regarding those images.

Here are my models: From models.py:

class ItemModel(models.Model):
    stock_number = models.IntegerField(blank=False)
    title = models.CharField(max_length=255, blank=False)
    price = models.IntegerField(blank=False)
    description = models.TextField(max_length=1000)
    condition = models.CharField(max_length=255, choices=COND_CHOICES, blank=False)
    model = models.CharField(max_length=255)
    dimensions = models.CharField(max_length=255)
    make = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title


class Images(models.Model):
    item = models.ForeignKey(ItemModel, on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to=images_directory_path)

Update: Here's the function i'm using to rename images:

def images_directory_path(instance, filename):
    return '/'.join(['images', str(instance.item.id), str(uuid.uuid4().hex + ".png")])

And here's I'm trying to access these images for an item in the template:

{% for img in item.images.all %}
    {% if forloop.first %}
        <td> {{ item.images.first.name }}</td>
    {% endif %}
{% endfor %}

Here's the view:

From views.py:

class Inventory(View, LoginRequiredMixin):
    def get(self, request):
        items = None
        try:
            items = ItemModel.objects.all()
        except ItemModel.DoesNotExist:
            return redirect('home')

        return render(request, 'app/inventory.html', {'all_items': items})

what can be wrong here?


Solution

  • Looks like you just need

    {% for img in item.images.all %}
        {% if forloop.first %}
            <td> <img src="{{ img.image.url }}"/> </td>
        {% endif %}
    {% endfor %}