Using session data to populate multiple forms

129 views
Skip to first unread message

Aaron Reabow

unread,
Jun 24, 2014, 9:30:48 AM6/24/14
to django...@googlegroups.com
Hi All,

I have been scratching around this for a bit now.

Sometimes I don't want people to have to register on my site, but I do want to capture some information about them and reuse it across multiple mini questionnaires/forms.

I can happily set and get a session variable using the session 'dictionary'

What I am trying to figure out is how to then use this information to populate fields so I can know who is giving which answers.

I could take the session variable and write this to the template, grab this information in the template and write it to the input using jquery.  This doesn't seem the most efficient way.

I assume writing this information in the view would be optional, alternatively is there a way to foreignkey type of approach.

Also I am pretty new to django so i might be way off the mark..

many thanks in advance,

Aaron

my view:

def sessionStoreView(request):
    if request.method == "POST":
        form = sessionStoreForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance['participant'] = request.session.get('participant') #something like that would be great if it worked
            model_instance.save()
            return HttpResponseRedirect('sessionStore')
    else:
        form = sessionStoreForm()
    return render(request, "sessionStore.html", {'form': form})

Daniel Roseman

unread,
Jun 24, 2014, 12:04:16 PM6/24/14
to django...@googlegroups.com
Apart from the fact that model_instance is, well, a model instance, and therefore you access it via dot notation not dictionary notation, what exactly are you having trouble with here?
--
DR. 

Aaron Reabow

unread,
Jun 24, 2014, 12:21:02 PM6/24/14
to django...@googlegroups.com
Hi Daniel,

Thanks for the response. 

What I am trying to get right is the ability to save session data across multiple forms.

In an initial form, people can tell me some generic information about their department, role etc.

It is then useful for me to save that information alongside their answers in subsequent forms that they complete within a session.  (It helps me to know who is who when I analyse their responses)

I save a little session dictionary of their core information in URL1 and am looking to add that information to the models that they complete in URL2,3,4...

I hope that makes some sense.

warm regards,

Aaron

Aaron Reabow

unread,
Jun 25, 2014, 3:58:05 AM6/25/14
to django...@googlegroups.com
Perhaps I should be more specific.

How do i populate some fields in a table from a form, and other fields from session variables in the view  (or the template or the model if that was more appropriate)

many thanks,

aaron

Daniel Roseman

unread,
Jun 25, 2014, 5:07:19 AM6/25/14
to django...@googlegroups.com
On Wednesday, 25 June 2014 08:58:05 UTC+1, Aaron Reabow wrote:
Perhaps I should be more specific.

How do i populate some fields in a table from a form, and other fields from session variables in the view  (or the template or the model if that was more appropriate)

many thanks,

aaron


Well, I understand that bit. What I don't understand is what's wrong with the code you've posted, once you correct the use of dictionary syntax?  This is exactly the correct approach.
--
DR.

Aaron Reabow

unread,
Jun 25, 2014, 9:24:12 AM6/25/14
to django...@googlegroups.com
haha - okay

model_instance.participant = request.session.get('participant')

works perfectly.

I thought the incorrect use of objects was just the start :)

Thanks Daniel, much appreciated.
Reply all
Reply to author
Forward
0 new messages