1
當前,網頁顯示按距離(類似於ListView)排序的搜索結果列表。我想要做的是創建一個過濾器來進一步縮小這些結果。使用基於功能的視圖過濾Geodjango結果
Views.py
def teacher_list(request, **kwargs):
if request.method == 'GET':
form = LocationForm(request.GET)
if form.is_valid():
SearchPoint = Point(form.cleaned_data['Lng'], form.cleaned_data['Lat'])
Radius = form.cleaned_data['SearchRadius']
Type = form.cleaned_data['Type']
else:
form = LocationForm()
SearchPoint = Point(0, 0)
Radius = form.cleaned_data['SearchRadius']
else:
form = LocationForm()
SearchPoint = Point(0, 0)
Radius = form.cleaned_data['SearchRadius']
results = Teacher.objects.filter(location__distance_lte=
(SearchPoint, D(km=Radius)))\
.annotate(distance=Distance('location', SearchPoint))\
.order_by('distance')
return render(request, "users/teacher_list.html", context={"form": form,"teacher_list":results,})
目前正在過濾的作品,而不是分類變量。例如,如果我更改位置或搜索半徑,表單會更新並顯示新結果。
但是,我有一個名爲TYPE
的分類變量,它可以是FREE或PAID。如果一個人選擇一個免費的過濾器,只顯示空閒的結果,反之亦然。這些是我模型中的布爾字段。
class Teacher(models.Model):
free = models.BooleanField()
paid = models.BooleanField()
我嘗試添加是某種過濾參數,將同樣努力
if Type == 'Free':
filter_variable = 'free=True'
elif Type == 'Paid':
filter_variable = 'paid=True'
else:
filter_variable =''
new_result_set = results.filter(filter_variable)
這是一種有效的方式來過濾?我看着Django過濾器,但看起來沒有geodjango兼容,似乎是我需要的矯枉過正。
非常感謝。有沒有什麼方法可以在template.html中將選定的標籤放在選項上,以便它反映當前選定的標籤。例如。 – Roma
我想是的,但我不明白你的問題的細節。你可以嘗試在SO上搜索它,如果不成功,寫一個問題。 –
謝謝,我需要做一些研究並嘗試代碼。如果我不明白,我會問新的。謝謝 – Roma