만약 인원수를 검색한다면 그 인원과 같거나 그보다 많은 인원을 수용할 수 있는 방을 제공해줘야 한다.
rooms - views.py
...
filter_args = {}
if city != "Anywhere":
filter_args["city__startswith"] = city
print("filter_args : ", filter_args)
rooms = models.Room.objects.filter(**filter_args)
print("rooms : ", rooms)
...
그러하다. 잘 찾았다.
잘 찾는걸 확인했으니 이걸 context로 넣어주자.
room - views.py
return render(
request,
"rooms/search.html",
context={**form, **choices, "rooms":rooms},
)
templates - rooms - search.html
...
{% for room in rooms %}
<h4>{{ room.name }}</h4>
{% endfor %}
{% endblock content %}
이제 국가 정보로도 검색이 되게 해보자.
rooms - views.py
...
filter_args = {}
if city != "Anywhere":
filter_args["city__startswith"] = city
filter_args["country"] = country
rooms = models.Room.objects.filter(**filter_args)
return render(
request,
"rooms/search.html",
context={**form, **choices, "rooms": rooms},
)
choices라서 그런진 모르겠는데 country의 경우 __startswith으로 입력하면 적용이 안된다.
다음으로 room_type을 해볼건데 이건 일단 스스로 해봤다.
if room_type != 0:
filter_args["room_type"] = room_types[room_type]
되기야 잘 된다.
인강에서는 이렇게 했다.
if room_type != 0:
filter_args["room_type__pk__exact"] = room_type
아래의 방법을 사용한 듯 하다.
https://docs.djangoproject.com/en/4.0/topics/db/queries/#the-pk-lookup-shortcut
잠시 확인할게 있다.
Shared Room 2개와 Entire Place 1개
ㅇㅋ 괜찮네.
다음은 가격.
if price != 0:
filter_args["price__lte"] = price
같은 원리로 숫자들어가는 다 처리하자.
if guests != 0:
filter_args["guests__gte"] = guests
if bedrooms != 0:
filter_args["bedrooms__gte"] = bedrooms
if beds != 0:
filter_args["beds__gte"] = beds
if baths != 0:
filter_args["baths__gte"] = baths
마지막으로 booleanfield
if instant is True:
filter_args["instant_book"] = True
if superhost is True:
filter_args["host__superhost"] = True
근데 보면 instant book이 Shared Room으로 된 2개는 모두 된다고 나와있는데 이게 적용이 안된다. 적용해도 해제해도 검색하면 다 뜬다.
첫번째 url에 힌트가 있는데 True가 아니라 on으로 뜬다. 아마 string으로 되어서 그런가본데 이거를 boolean으로 바꿔주겠다.
이거 확인할 때 주의해야하는게 저거를 체크하면 해당 조건을 +alpha로 보여주는거고 체크가 안되어있으면 해당 조건을 제외하고 보여주는 것이다.
따라서 둘다 superhost일 경우 체크를 하던 안하던 둘다 뜬다. instant_book도 마찬가지.
둘 중 하나만 superhost로 해봤더니 아래와 같이 걸러진다.
이런 것들은 어떻게 출력되나 확인해보자.
print(s_amenities)
이 경우 for문을 활용하자.
일단 스스로 해봤다.
if len(s_amenities) != 0:
for a in s_amenities:
filter_args["amenities__pk__exact"] = int(a)
강의도 비슷하다.
if len(s_amenities) != 0:
for a in s_amenities:
filter_args["amenities__pk__exact"] = int(a)
URL은 이렇게 나온다.
후 빡셋다