I am currently working an an anonymous questionnaire which contains several questions in different views.
As it is anonymous I think the best approach is to use the native session framework of Django.
So I plan to store the user responses as cookies on the server side.
Example, the following two lines will store a cookie on the server side containing the question 1 response and prompt the user browser to set a session ID cookie, so that the next question will be correctly linked with the session (using the session ID provided by the client’s browser):
request.session['question 1'] = 'response from the user'
HttpResponse('Thank you for your submit')
Then on my admin view, I will iter over the Session objects (from django.contrib.sessions.models.Session) and get the responses.
The problem I see, is that the cookies are not structured data, it’s only string, if the user response should be a date or any more complex data structure I cannot modeling it.
Is there a simple possibility to define my data models and link it with the current anonymous session ?
If you want a way to “link” the data from your session to a model that you have, I think a feasible way to do so is storing the primary key from the model you are trying to connect:
But I would recommend storing simple data using sessions and connect them to a model instance being stored in a regular database. That’s the idea behind user session in Django.
If you want something ready to use, django-formtools might be the library you are looking for: