Wednesday, June 25, 2014

Re: Using session data to populate multiple forms

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

On Tuesday, 24 June 2014 18:21:02 UTC+2, Aaron Reabow wrote:
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

On Tuesday, 24 June 2014 18:04:16 UTC+2, Daniel Roseman wrote:
On Tuesday, 24 June 2014 14:30:48 UTC+1, Aaron Reabow wrote:
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})

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. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c43f521c-6765-457f-9fe1-6f851ebaae1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment