1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def my_view(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# ...
return HttpResponseRedirect('/thanks/')
else:
form = MyForm()
# Load initial form fields from GET parameters
for key in request.GET:
try:
form.fields[key].initial = request.GET[key]
except KeyError:
# Ignore unexpected parameters
pass
return render_to_response('my_template.html', {'form': form})
|
More like this
- SeparatedValuesField by jezdez 5 years, 5 months ago
- Formset Form by stephen_mcd 3 years, 2 months ago
- Extend generic view object_list to support paginate_by via cookies by kersurk 2 years, 4 months ago
- Tags & filters for rendering search results by exogen 5 years, 1 month ago
- Preview tag for fields with choices by kmmbvnr 5 years, 1 month ago
Comments
Last time I did this, all I had to do was:
#
Oh, good point. That's quite a bit easier. I can still see some merit to this technique if you want different error handling or you want to exclude certain GET parameters. Thanks for the insight. I wish this technique was explained in the docs.
#
I'd use
Here I used "None" to hide errors when no fields has been filled.
#
I'm back to using the method I described originally. Using MyForm(request.GET) is triggering form validation, and MyForm(initial=request.GET) is resulting in lists getting printed in the inputs ("[u'somevalue']") when using ModelForms. Thanks for the downvotes anyway.
#