如何在 Django 中使用 GET 请求后用户发送的数据填充我的表单?
How do I populate my form with the data sent by a user after a GET request in Django?
def index(request):
if len(request.GET) == 0:
form = FlightSearchForm()
else:
arr = request.GET.get('arrival_airport')
dep = request.GET.get('departure_airport')
form = FlightSearchForm({'departure_airport': dep,'arrival_airport': arr})
return render(request, 'app1/index.html', {'form': form})
我想做的是创建一个允许用户搜索航班的表单,因此我需要他们使用 GET 请求。问题是我不知道如何在他们转到特定 url(包含他们搜索的所有详细信息)后让他们的搜索出现在字段中。它过去曾与 POST 请求一起工作,但显然我不能在这种情况下使用它。
class FlightSearchForm(forms.Form):
def __init__(self, *args, **kwargs):
self.departure_airport = kwargs.pop('departure_airport')
self.arrival_airport = kwargs.pop('arrival_airport')
super.__init__(*args, **kwargs)
departure_airport = forms.CharField(max_length=100)
arrival_airport = forms.CharField(max_length=100)
我试过类似的方法,但没有用。现在它大声说有一个不存在的键错误。我该怎么办?
你可以创建一个没有额外逻辑的表单 departure_airlport
和 arrival_airport
,所以很简单:
class FlightSearchForm(forms.Form):
departure_airport = forms.CharField(max_length=100)
arrival_airport = forms.CharField(max_length=100)
# <strong>no __init__</strong>
然后你可以使用数据作为initial=…
[Django-doc]数据:
def index(request):
if not request.GET:
form = FlightSearchForm()
else:
arr = request.GET.get('arrival_airport')
dep = request.GET.get('departure_airport')
form = FlightSearchForm(<strong>initial={'departure_airport': dep,'arrival_airport': arr}</strong>)
return render(request, 'app1/index.html', {'form': form})
另一种选择是将 request.GET
作为数据直接传递到表单中:
def index(request):
if not request.GET:
form = FlightSearchForm()
else:
form = FlightSearchForm(<b>request.GET</b>)
return render(request, 'app1/index.html', {'form': form})
def index(request):
if len(request.GET) == 0:
form = FlightSearchForm()
else:
arr = request.GET.get('arrival_airport')
dep = request.GET.get('departure_airport')
form = FlightSearchForm({'departure_airport': dep,'arrival_airport': arr})
return render(request, 'app1/index.html', {'form': form})
我想做的是创建一个允许用户搜索航班的表单,因此我需要他们使用 GET 请求。问题是我不知道如何在他们转到特定 url(包含他们搜索的所有详细信息)后让他们的搜索出现在字段中。它过去曾与 POST 请求一起工作,但显然我不能在这种情况下使用它。
class FlightSearchForm(forms.Form):
def __init__(self, *args, **kwargs):
self.departure_airport = kwargs.pop('departure_airport')
self.arrival_airport = kwargs.pop('arrival_airport')
super.__init__(*args, **kwargs)
departure_airport = forms.CharField(max_length=100)
arrival_airport = forms.CharField(max_length=100)
我试过类似的方法,但没有用。现在它大声说有一个不存在的键错误。我该怎么办?
你可以创建一个没有额外逻辑的表单 departure_airlport
和 arrival_airport
,所以很简单:
class FlightSearchForm(forms.Form):
departure_airport = forms.CharField(max_length=100)
arrival_airport = forms.CharField(max_length=100)
# <strong>no __init__</strong>
然后你可以使用数据作为initial=…
[Django-doc]数据:
def index(request):
if not request.GET:
form = FlightSearchForm()
else:
arr = request.GET.get('arrival_airport')
dep = request.GET.get('departure_airport')
form = FlightSearchForm(<strong>initial={'departure_airport': dep,'arrival_airport': arr}</strong>)
return render(request, 'app1/index.html', {'form': form})
另一种选择是将 request.GET
作为数据直接传递到表单中:
def index(request):
if not request.GET:
form = FlightSearchForm()
else:
form = FlightSearchForm(<b>request.GET</b>)
return render(request, 'app1/index.html', {'form': form})