Issue
So I am building a site with Django where a Property
has an agent
who lists it, in the AgentDetailView
I am rendering all the agent's information but I also want to render the agent's listed properties in the same page. So I was trying the Property.objects.filter
method but to no success.
How can I make my code work? Thanks in advance!
My models.py:
class Property(models.Model):
price = models.IntegerField()
address = models.CharField(max_length = 10000)
state = models.CharField(max_length = 1000)
country = models.CharField(max_length = 1000)
description = models.TextField()
image1 = models.ImageField(upload_to = 'Images/')
image2 = models.ImageField(upload_to = 'Images/')
image3 = models.ImageField(upload_to = 'Images/')
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
agent_description = models.TextField()
is_featured = models.BooleanField(default = False)
bedrooms = models.IntegerField()
bathrooms = models.IntegerField()
date_posted = models.DateTimeField(default=timezone.now)
slug = models.SlugField(default='')
class Agent(models.Model):
name = models.CharField(max_length=100)
password = models.CharField(max_length=100)
image = models.ImageField(upload_to = 'Images/')
bio = models.TextField()
instagram = models.URLField(max_length=100)
twitter = models.URLField(max_length=100)
facebook = models.URLField(max_length=100)
linkedin = models.URLField(max_length=100)
is_featured = models.BooleanField(default = False)
slug = models.SlugField(default='')
My views.py:
class AgentDetailView(DetailView):
model = Agent
template_name = 'blog/agent_detail.html'
slug_field = 'slug'
def get_queryset(self):
return Property.objects.filter(agent=self.kwargs.get('agent'))
Solution
I think there's an issue in your get_queryset
method. The agent
field in the Property
model is a foreign key to the Agent
model, so you should filter properties based on the specific agent instance rather than the agent slug.
Try:
from django.shortcuts import get_object_or_404
class AgentDetailView(DetailView):
model = Agent
template_name = 'blog/agent_detail.html'
slug_field = 'slug'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
agent = self.get_object()
properties = Property.objects.filter(agent=agent)
context['properties'] = properties
return context
The get_context_data
method is used to add extra context data to the template. It retrieves the specific agent instance using self.get_object()
and then filters the properties based on that agent.
Now, in your agent_detail.html
template, you can access the agent details using {{ agent.field_name }}
and the properties using a for loop, for example:
<!-- Display agent details -->
<h1>{{ agent.name }}</h1>
<p>{{ agent.bio }}</p>
<!-- Display properties listed by the agent -->
<h2>Listed Properties:</h2>
<ul>
{% for property in properties %}
<li>{{ property.address }} - {{ property.price }}</li>
{% endfor %}
</ul>
Answered By - Yes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.