Django:我如何使用前一个表单中的对象来过滤对象?

时间:2018-06-03 15:30:02

标签: python django python-3.x

def Date_Form(request):
    if request.method == 'POST':
        form = DateForm(request.POST)

        if form.is_valid():
            date = form.cleaned_data['date']

            date1 = DateOptions.objects.get(date=date)
            flight_date = Flight.objects.filter(date_options=date1)

            form1 = KeyWordForm(request.POST)

            return render(request, 'app/index.html',{'flight_date': flight_date, 'date': date, 'form1': form1})

    else:
        form = DateForm()

    return render(request, 'app/options.html', {'form':form})


def Search_Destination(request):
    if request.method =='POST':
        form1 = KeyWordForm(request.POST)

        if form1.is_valid():
            destination = form1.cleaned_data['destination'].capitalize()

            result= Flight.objects.filter(destination__icontains=destination, date_options=????)

            return render(request, 'app/search.html', {'result':result, 'destination':destination})

    else: 
        form1 = KeyWordForm()

    return render(request, 'app/searchfield.html', {'form1':form1})

这部分是我奋斗的地方:

result= Flight.objects.filter(destination__icontains=destination, date_options=????)

我希望能够使用与date1相同的date_option。我知道我不能说date_options = date1。系统无法识别它。

2 个答案:

答案 0 :(得分:1)

首次使用plot(x, observation.based = TRUE, major.ticks = "hours") 实例时,您可以使用session存储pk

date1

在第二个视图中,您将检索date1 = DateOptions.objects.get(date=date) flight_date = Flight.objects.filter(date_options=date1) # Set the session variable request.session['date1_pk'] = date1.pk ,并使用它进行过滤

pk

答案 1 :(得分:-1)

好的,我将回答我自己的问题。大声笑我刚刚找到答案。我需要将date1定义为全局对象,以便Python识别范围内的所有对象。

def Date_Form(request):
    if request.method == 'POST':
        form = DateForm(request.POST)

        if form.is_valid():
            date = form.cleaned_data['date']
            global date1
            date1 = DateOptions.objects.get(date=date)
            flight_date = Flight.objects.filter(date_options=date1)

            form1 = KeyWordForm(request.POST)

            return render(request, 'app/index.html',{'flight_date': flight_date, 'date': date, 'form1': form1})

    else:
        form = DateForm()

    return render(request, 'app/options.html', {'form':form})


def Search_Destination(request):
    if request.method =='POST':
        form1 = KeyWordForm(request.POST)

        if form1.is_valid():
            destination = form1.cleaned_data['destination'].capitalize()

            result= Flight.objects.filter(destination__icontains=destination, date_options=date1)

            return render(request, 'app/search.html', {'result':result, 'destination':destination})

    else: 
        form1 = KeyWordForm()

    return render(request, 'app/searchfield.html', {'form1':form1})