@limedaring Hi Tracy, I cannot seem to get the images to work from your example in your IC book. I can upload images in admin and it creates the folder, so it seems everything works well, I just can’t get it to work in my template. I named my upload class differently than in book since I needed to provide upload classes for a few separate Models, so naming them differently was needed - not sure if this is part of my issue. Can you please take a peak at my code below and see if there is anything that stands out? Thank you for your help.
model
class Tool(Timestamp):
model_number = models.ForeignKey(ModelNumber)
price = models.SmallIntegerField()
title = models.CharField(max_length=250)
slug = models.SlugField()
description = RichTextField()
type = models.ForeignKey(Type)
category = models.ForeignKey(Category)
supplies = models.ManyToManyField(Part, blank=True)
class Meta:
verbose_name = 'Tool'
verbose_name_plural = 'Tools'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("product_detail", kwargs={"category": self.category, "slug": self.slug})
# Def for image path
def tool_image_path(instance, filename):
return '/'.join(['tool_images', instance.tool.slug, filename])
# Upload class for Tools
class ToolUpload(models.Model):
tool = models.ForeignKey(Tool, related_name="uploads")
image = models.ImageField(upload_to=tool_image_path)
class Meta:
verbose_name = 'Tool Image Upload'
verbose_name_plural = 'Tool Image Uploads'
view
def tool_list(request):
tools = Tool.objects.all()
parts = Part.objects.all()
tool_uploads = tools.uploads.all()
return render(request, 'tool_list.html', {'tools': tools, 'parts': parts, 'tool_uploads': tool_uploads})
It generates an error in the browser as 'QuerySet' object has no attribute 'uploads'