request.GET - the dictionary as an object.
initial only works in the case of an unrelated form.
Forms have an attribute called data . This attribute is provided as the first positional argument or as an argument to the data keyword during form initialization.
Linked forms are those in which you provide some data as the first argument of the form, and the unbound form has the data attribute set to None.
Here, in your form initialization form=Form(request.GET) , you provide the first positional argument, so the data attribute is set on the form and becomes the associated form. This happens even if request.GET is an empty dictionary. And since your form becomes a linked form, therefore, the initial name field does not affect it.
So, in a GET request, you should either do:
form = Form()
and the initial of name field will be executed.
Or, if you want to read name with request.GET, and if you need to use it instead of the original one, then your view has the following.
name = request.GET.get(name) form_level_initial = {} if name: form_level_initial['name'] = name form = Form(initial=form_level_initial)
source share