我正在尝试根据Django 2.0中的日期过滤游览,即显示从2019年6月1日或之后开始的所有游览(应从表格中接收)。 我有一个“游览”模型:
class Tour(models.Model):
destination = models.ForeignKey(Destination, on_delete=models.PROTECT)
Created = models.DateTimeField(auto_now_add=True)
Updated = models.DateTimeField(auto_now=True)
Available = models.BooleanField(default=True, help_text="Tick it if you still operate this tour")
我有一个模型“出发”:
class Departures(models.Model):
tour = models.ForeignKey(Tour, on_delete=models.CASCADE)
DepartingDate = models.DateField(auto_now=False)
FinishingDate = models.DateField(auto_now=False)
DepartureStatus = models.CharField(max_length=200, choices = DEPARTURE_STATUS, help_text="Select from the list")
PlacesAvailable = models.BooleanField("Places avalable", default="True")
DepartureGaranteed = models.BooleanField("Departure Garanteed", default="True")
我希望用户从html表单中选择游览的开始日期:
<div class="form-group">
<input class="form-control" id="dateStart" name="DateStart" type="date"/>
</div>
models.py中有一个表格:
class SearchForm(forms.Form):
Country = forms.CharField(max_length=255, required=False)
TourType = forms.CharField(max_length=255, required=False)
TourTheme = forms.CharField(max_length=255, required=False)
Duration = forms.IntegerField(required=False) #, blank=True
Price = forms.CharField(max_length=255, required=False) #, blank=True
DateStart = forms.DateField(required=False) #input_formats='%d/%m/%Y',
编辑(添加了视图功能):
def search_tours(request):
if request.method == 'GET':
form = SearchForm(request.GET)
if form.is_valid():
Country = form.cleaned_data['Country']
TourType = form.cleaned_data['TourType']
TourTheme = form.cleaned_data['TourTheme']
Duration = form.cleaned_data['Duration']
Price = form.cleaned_data['Price']
DateStart = form.cleaned_data['DateStart']
#Convert the date format
# DateStart = datetime.strptime(str(DateStart), '%Y-%m-%d')
# DateStart.strftime('%Y/%m/%d')
# DateStart.strftime("%Y-%m-%d")
departures = Departures.objects.all()
myfilter = Q()
if Country is not None:
for c in Country:
myfilter &= Q(destination__Country__icontains=c)
if TourType is not None:
myfilter &= Q(TourType__contains=TourType)
if TourTheme is not None:
myfilter &= Q(TourTheme__contains=TourTheme)
if Duration is not None:
myfilter &= Q(Duration__lte=Duration)
if Price in request.GET:
myfilter &= Q(Price__lte=Price)
if DateStart in request.GET:
myfilter &= Q(Departures__DepartingDate__gt=DateStart)
tours = Tour.objects.filter(myfilter)
paginator = Paginator(tours, 3) # Show 3 tours per page
page = request.GET.get('page')
tours = paginator.get_page(page)
args = {'tours': tours, 'departures': departures,
'Country': Country,
'TourType': TourType,
'TourTheme': TourTheme,
'Price' : Price,
'Duration' : Duration,
}
return render(request, 'tours/search_tours.html', args)
else:
return HttpResponseRedirect('../error/')
else:
form = SearchTourForm()
tours = Tour.objects.filter(Available = True)
return render(request, 'tours/search_tours.html', {
'tours': tours,
})
因此,我尝试过滤如下所示的游览:
tours = Tour.objects.filter(Q(Departures__DepartingDate__gt=DateStart))
但是它不起作用。我想问题是html表单日期格式和django db日期格式不匹配。它不会给出错误,但是不会过滤。只显示所有游览。
如何将这两个日期转换为相同的格式并进行比较?如何解决此错误?
答案 0 :(得分:0)
所以我要解决的问题是我将模型名称从“出发”更改为“出发”:
if DateStart is not None:
myfilter &= Q(departures__DepartingDate__gt=DateStart)
我还从
更改了上述if语句的条件if DateStart in request.GET:
到
if DateStart is not None:
玩得开心! ;)