Question

Is there a way that i can access the values in my form fields before submitting the form.

I need to store the value of one of my fields in request.session[] so as to access it later ( in the same view). I tried doing it using request.GET but it always none.

request.GET.get('name')

where name is the field in a model.

Update

I want to store the value of the form field which is random value generated every time the form is displayed. My models.py contains a random() method which is the default value of the field.

I want to store the field value in sessions, so that i can get the same field value after i return to the page after navigating a few more pages from that page

This is what i was doing:

Django request.session does not resolve

Was it helpful?

Solution

First of all careful when using random in models:

Random/non-constant default value for model field?

Once this is clear I suppose you initialise the form in the view, is there that you need to access the form and get the value for put it in session. For example

form = MyModelForm();
request.session['your_session_key'] = form.data['field_name']

As far as I understand in some cases you want to set this value in the form from the session instead, for do that you can use the initial data as described here.

https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top