我目前正在做一个Django项目,我希望用户使用不同的标准来执行物业搜索,如物业类型,州,最小卧室,浴室类型,最低和最高价格。我希望搜索是灵活的,以便用户选择哪个字段搜索,而不是强迫他输入每个表单字段在搜索。我已经尽力了,但没有错误,也没有积极的结果,甚至当用户搜索匹配模型记录时,它仍然不显示它。以下是我的模型代码:
class Property(models.Model):
PROPERTY_TYPE_CHOICES = [
('Complete House', 'Complete House'),
('Apartment', 'Apartment'),
('Self-Contained', 'Self-Contained'),
]
BEDROOM_CHOICES = [
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5+'),
]
BATHROOM_CHOICES = [
('Self-contained', 'Self-contained'),
('General', 'General'),
('Private Detached', 'Private Detached'),
]
COUNTRY_CHOICES = [
('Nigeria', 'Nigeria'),
]
STATE_CHOICES = [
('Abia', 'Abia'),
('Adamawa', 'Adamawa'),
('Akwa Ibom', 'Akwa Ibom'),
('Anambra ', 'Anambra '),
('Bauchi', 'Bauchi'),
('Bayelsa', 'Bayelsa'),
('Benue ', 'Benue '),
('Borno', 'Borno'),
('Cross River', 'Cross River'),
('Delta', 'Delta'),
('Ebonyi', 'Ebonyi'),
('Edo', 'Edo'),
('Ekiti', 'Ekiti'),
('Enugu', 'Enugu'),
('Gombe', 'Gombe'),
('Imo', 'Imo'),
('Jigawa', 'Jigawa'),
('Kaduna', 'Kaduna'),
('Kano', 'Kano'),
('Katsina', 'Katsina'),
('Kebbi', 'Kebbi'),
('Kogi', 'Kogi'),
('Kwara', 'Kwara'),
('Lagos', 'Lagos'),
]
agent = models.ForeignKey(Agent, on_delete=models.SET_NULL, blank=True, null=True)
landlord = models.ForeignKey(Landlord, on_delete=models.SET_NULL, blank=True, null=True)
description = models.CharField(max_length=60, blank=True, null=True)
property_type = models.CharField(max_length=20, choices=PROPERTY_TYPE_CHOICES)
bedrooms = models.CharField(max_length=2, blank=True, null=True, choices=BEDROOM_CHOICES)
bathroom_type = models.CharField(max_length=20, choices=BATHROOM_CHOICES)
country = models.CharField(max_length=20, choices=COUNTRY_CHOICES)
state = models.CharField(max_length=20, choices=STATE_CHOICES)
state_lga = models.CharField(max_length=12, blank=True, null=True)
address = models.CharField(max_length=60, null=True, blank=True)
latitude = models.FloatField(null=True, blank=True)
longitude = models.FloatField(null=True, blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
is_available = models.BooleanField(default=True)
image = models.ImageField(default='avatar.jpg', blank=False, null=False, upload_to ='profile_images',
)
last_updated = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.description
表格代码:
class SearchForm(forms.Form):
property_type = forms.ChoiceField(choices=Property.PROPERTY_TYPE_CHOICES, required=False)
state = forms.ChoiceField(choices=Property.STATE_CHOICES, required=False)
min_beds = forms.CharField(max_length=10, required=False, widget=forms.TextInput(attrs={'placeholder': "Min Bedrooms"}))
bathroom_type = forms.ChoiceField(choices=Property.BATHROOM_CHOICES, required=False)
min_price = forms.DecimalField(max_digits=10, decimal_places=2, required=False, widget=forms.TextInput(attrs={'placeholder': "Min Price"}))
max_price = forms.DecimalField(max_digits=10, decimal_places=2, required=False, widget=forms.TextInput(attrs={'placeholder': "Max Price"}))
查看代码:
# Home Page View.
def index(request):
listings = Property.objects.filter(is_available=True).order_by('-last_updated')[:6]
form = SearchForm(request.GET or None)
if request.method == 'GET' and form.is_bound:
if form.is_valid():
cleaned_data = form.cleaned_data
property = cleaned_data.get('property_type')
state = cleaned_data.get('state')
min_beds = cleaned_data.get('min_beds') or 0
bathroom = cleaned_data.get('bathroom_type')
min_price = cleaned_data.get('min_price')
max_price = cleaned_data.get('max_price')
# Filter properties based on search parameters
properties = Property.objects.filter(is_available=True)
if property:
properties = properties.filter(property_type=property)
if state:
properties = properties.filter(state=state)
if min_beds:
properties = properties.filter(bedrooms__gte=min_beds)
if bathroom:
properties = properties.filter(bathroom_type=bathroom)
if min_price:
properties = properties.filter(price__gte=min_price)
if max_price:
properties = properties.filter(price__lte=max_price)
# Apply sorting
properties = properties.order_by('-last_updated')[:6]
context = {
'listings': properties,
'page_title': 'Ease to Locate Ease to Rent... | UyaProp',
'form': form,
}
return render(request, 'pages/index.html', context)
context = {
'listings': listings,
'form': form,
}
return render(request, 'pages/index.html', context)
HTML代码:
<div class="search-property">
<form method="GET" action="{% url 'pages-index' %}">
{% csrf_token %}
<div class="row">
<div class="col-md-3">
<div class="single-search-property">
<div class="intro">
{{ form.property_type }}
</div>
</div>
</div>
<div class="col-md-3">
<div class="single-search-property">
<div class="intro">
{{ form.state }}
</div>
</div>
</div>
<div class="col-md-3">
<div class="single-search-property">
{{ form.min_beds }}
</div>
</div>
<div class="col-md-3">
<div class="single-search-property">
<div class="intro">
{{ form.bathroom_type }}
</div>
</div>
</div>
<div class="col-md-3">
<div class="single-search-property">
{{ form.min_price }}
</div>
</div>
<div class="col-md-3">
<div class="single-search-property">
{{ form.max_price }}
</div>
</div>
<div class="col-md-3">
<div class="single-search-property button">
<button type="submit">Search</button>
</div>
</div>
</div>
</form>
</div>
1条答案
按热度按时间ddhy6vgd1#
这是一个非常乏味的方式来归档搜索功能,而不是手动编写搜索过滤器,你使用**
pip install django-filter
**库。"Django-filter是一个可重用的Django应用程序,允许用户以声明的方式添加来自URL参数的动态QuerySet过滤。"
我的github仓库链接here
浏览器输出