1👍
As specified by the documentation on initial=…
:
(…) This is why initial values are only displayed for unbound forms. For bound forms, the HTML output will use the bound data.
So only for unbounded the forms (without request.POST
passed as data), it will make use of the initial
.
You thus can make a GET request with initial data, and if the user does not alter these, and makes a POST request, then these initial data will be part of the POST request.
def home(request):
blablaTrips={}
skyscannerTrips={}
trips_busTrain={}
if request.method == 'POST':
form = userRequest(request.POST)
if form.is_valid():
print((form.cleaned_data))
else:
# ↓ this form will work with the initial data
form = UserRequest()
return render(request, 'some-template.html', {'form': form})
Source:stackexchange.com