Reputation: 92
I'm making a simple real estate app where users can create Estates. The Estate model has one main image, but the user can upload multiple images using another model and form for that. On the estate details page which is a DetailView, I want to display the images and info about the user who is the owner of the current Estate. I'm passing the above info using get_context_data(). While debugging everything seems fine to me the context contains the info about the user and the images but this information is not rendered. Can you please help me with this issue?
my user model is linked to the current estate with the following:
user = models.ForeignKey(
UserModel,
on_delete=models.CASCADE,
)
This is the EstateImagesModel
class EstateImages(models.Model):
IMAGE_UPLOAD_TO_DIR = 'estates/'
estate = models.ForeignKey(
Estate,
related_name='images',
on_delete=models.CASCADE,
)
image = models.FileField(
upload_to=IMAGE_UPLOAD_TO_DIR,
)
and the detail view is
class EstateDetailsView(views.DetailView):
model = Estate
template_name = 'main/estates/estate_details.html'
context_object_name = 'estate'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_owner'] = self.object.user == self.request.user # this works fine
estate_images = list(EstateImages.objects.filter(estate_id=self.object.id)) # returns a [<EstateImages: EstateImages object (10)>]
seller = Profile.objects.filter(user_id=self.object.user_id) # returns a <QuerySet [<Profile: Profile.First Name Profile.Last Name>]>
context.update({
'estate_images': estate_images,
'seller': seller,
})
return context
in the template, this is how I'm trying to access the context data
{% for image in estate_images %}
<div class="carousel-item-b swiper-slide">
<img src="{{ image.url }}" alt="">
</div>
{% endfor %}
And for the user:
<div class="property-agent">
<h4 class="title-agent">{{ seller.full_name }}</h4>
<p class="color-text-a">
{{ seller.description }}
</p>
Upvotes: 0
Views: 269
Reputation: 1879
The seller
is not rendered because you're passing a queryset to the context, not the actual model instance. I suggest changing the line where you define the seller to:
seller = Profile.objects.filter(user_id=self.object.user_id).first()
Edit: found the bug in the image rendering issue too:
{% for estate_image in estate_images %}
<div class="carousel-item-b swiper-slide">
<img src="{{ estate_image.image.url }}" alt="" />
</div>
{% endfor %}
Upvotes: 1