Modelesq
Modelesq

Reputation: 5382

Get session info and place into a django form

This form should not post, it's grabbing what is currently saved from the previous setup page. However, during this point in my setup: I'm just trying to get the session information and save it. In case a user decides to change their information.

What I've tried thus far:

What is my next move?

if request.method == 'POST':
...
else:
    profile = Profile.objects.get(user=request.user)
    initial = {}
    initial['first_name'] = request.user.first_name
    initial['last_name'] = request.user.last_name
    initial['email'] = request.user.email
    if profile:
        initial['about'] = profile.about
        initial['country'] = profile.country
    user_info_form = UserInfoForm(initial=initial)
context['user_info_form'] = user_info_form
context['profile'] = profile

Thanks for your help in advance.

Upvotes: 2

Views: 212

Answers (1)

supervacuo
supervacuo

Reputation: 9242

Almost there; try

request.session['form_data'] = user_info_form.data

This will be populated regardless of whether the form is valid or not. You can then put it back into a form on a subsequent request:

user_info_form = UserInfoForm(request.session['form_data'])

Upvotes: 1

Related Questions