Ok, now my python newbness is going to show... I figured out what was causing it. (but I don't know why it wasn't working before)
I can do the following: request.session['question_list'].append(question.id) but not: request.session['question_list'] = request.session['question_list'].append( question.id) at least it seems to work now. Also, just to note, this is without setting the session save every request or explicitly stating that the session has been modified. I'm going to continue to play with it and if it does break I'll report but it seems to be working well so far. -Chris On 7/26/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > What about setting session variable to None, then setting it back to a > new list object with the questions in it, just to see if it's a > problem with it not realizing that the list has been modified? > > I don't think this is the best solution either, but would be a good > test to see if it's just not realizing that the variable has changed, > and possibly another workaround if it works. > > > > On Jul 26, 1:09 pm, flynnguy <[EMAIL PROTECTED]> wrote: > > Thanks for responding, I should have mentioned previously that I did > > try putting 'SESSION_SAVE_EVERY_REQUEST=True' in my settings file but > > that didn't work. Now that I read your e-mail, I also tried setting > > request.session.modified = True right after my append but that didn't > > work either. > > > > I meant to reply to the list a while ago to tell my work around but I > > forgot, I guess now is as good a time as any... > > > > I ended up just using a string that I concatenate the new values to in > > a comma separated list: > > request.session['questions_asked'] += ','+str(question.id) > > (obviously you would have to initialize it for the first question) > > > > Then to convert that string to an actual list I do the following: > > for item in csv.reader([request.session['questions_asked']]): > > question_list = item > > > > I tried doing just: csv.reader([request.session['questions_asked']]) > > [0] but it's not a real list and in the interpreter, I get: TypeError: > > unsubscriptable object. So if anyone knows of a cleaner, more pythonic > > way, I'm all ears. (Because I'm guessing it's not very pythonic) > > > > So that's how I'm getting around it for now, I'd love to hear anyone > > else's thoughts on the subject. > > -Chris > > > > On Jul 26, 12:23 pm, "[EMAIL PROTECTED]" > > > > <[EMAIL PROTECTED]> wrote: > > > You might want to try adding: > > > request.session.modified = True ( or modify your settings file to save > > > every request by setting the SESSION_SAVE_EVERY_REQUEST to True ) > > > > > Because from this note from DjangoSessiondocs: > > > #Sessionis modified. > > > request.session['foo'] = {} > > > > > # Gotcha:Sessionis NOT modified, because this alters > > > # request.session['foo'] instead of request.session. > > > request.session['foo']['bar'] = 'baz' > > > > > On Jul 9, 10:50 am, flynnguy <[EMAIL PROTECTED]> wrote: > > > > > > Ok, I upgraded to the development version of django and that didn't > > > > help. I then started to try and trim out the code so it was of a > more > > > > manageable size to post here. It's still rather large but if it > helps > > > > anyone visualize what I'm talking about, here it is: > > > > > > ## test_indexs.html > > > > <h4>Take the Test</h4> > > > > <form action="." method="post"> > > > > <table> > > > > {{ form }} > > > > </table> > > > > <input type="submit" value="Take Test" /> > > > > </form> > > > > > > ## take_tests.html > > > > {{ questions_asked }} > > > > > > {% if score_page %} > > > > {{ num }} > > > > <h1 id="your_score">Your score is: {{ score|floatformat:2 }}% > > > > ({{ num_correct }}/{{ total }})</h1> > > > > <img id="score_tach" src="/flight/grade/tach.gif? > > > > score={{ score }}" /> > > > > {% else %} > > > > > > <h3>{{ question.id }} {{ question.question }}</h3> > > > > <form method="post" action="."> > > > > <input type="hidden" name="number_of_questions" value="{{ num > }}" / > > > > > > <ul> > > > > {% for answer in answers %} > > > > <li class="answers"><input type="submit" name="{{ answer.0}}" > > > > value="{{ answer.1 }}" /></li> > > > > {% endfor %} > > > > </ul> > > > > </form> > > > > {% endif %} > > > > > > ## views.py > > > > def sessiontest(request): > > > > if 'questionlist' inrequest.session: > > > > questions_asked =request.session['questionlist'] > > > > if request.method == "POST": > > > > ifrequest.session.get('num_questions',False): # If > there > > > > is a GET var called num_questions > > > > ifrequest.session['current_question'] <request.session > ['num_questions']: > > > > request.session['current_question'] += 1 > > > > question = Question.objects.order_by('?')[0] # > Get > > > > a random question > > > > request.session['previous_question'] = question > > > > answers = list(enumerate([question.correct_answer, > > > > question.other_answer_1, question.other_answer_2])) > > > > shuffle(answers) > > > > try: > > > > request.session['questionlist'] = > > > > questions_asked.append(int(question.id)) > > > > except AttributeError: > > > > request.session['questionlist'] = > > > > [(int(question.id)),] > > > > > > return render_to_response('take_tests.html', > > > > {'current_num':request.session['current_question'], \ > > > > > > 'num':request.session['num_questions'], \ > > > > > > 'question':question, \ > > > > > > 'first_page':False, \ > > > > > > 'questions_asked':questions_asked, \ > > > > > > 'answers': answers}) > > > > else: > > > > score = (float(request.session['num_correct'])/ > > > > float(request.session['num_questions']))*100 > > > > return render_to_response('take_tests.html', > > > > {'correct':request.session['num_correct'], \ > > > > > > 'total':request.session['num_questions'], \ > > > > > > 'score':score, \ > > > > > > 'score_page':True, \ > > > > > > 'num_correct':request.session['num_correct'] }) > > > > else: # No num_questions var set... show/process form > > > > form = TestForm(request.POST) > > > > if form.is_valid(): > > > > num_questions = > > > > form.cleaned_data['number_of_questions'] > > > > request.session['num_questions'] = num_questions > > > > request.session['current_question'] = 1 > > > > request.session['num_correct'] = 0 > > > > question = Question.objects.order_by('?')[0] # > Get > > > > a random question > > > > request.session['previous_question'] = question > > > > answers = list(enumerate([question.correct_answer, > > > > question.other_answer_1, question.other_answer_2])) > > > > shuffle(answers) > > > > request.session['previous_answers'] = answers > > > > request.session['questionlist'] = [int(question.id),] > > > > # topics = request.POST['topics'] > > > > return render_to_response('take_tests.html', > > > > {'current_num':request.session['current_question'], \ > > > > > > 'num':num_questions, \ > > > > > > 'first_page':True, \ > > > > > > 'questions_asked':request.session['questionlist'], \ > > > > > > 'question':question, 'answers': answers,}) > > > > else: > > > > # Remove num_questionssessionkey in case it is hanging > > > > around > > > > try: > > > > delrequest.session['num_questions'] > > > > except KeyError: > > > > pass > > > > form = TestForm() > > > > return render_to_response('test_indexs.html', {'form':form}) > > > > > > ###### End Code ####### > > > > > > If there is aproblemstoring lists in thesessionvariables, then I > > > > can look at some other method. I just thought this would be the best > > > > solution. Also it works some of the time, just not all of the time. > I > > > > don't understand why it's getting set to 'None'. > > > > -Chris > > > > > -- "If you vote for what you do not want, and you 'win', what have you won?" Ron Paul in 2008: http://www.ronpaul.org/ Read about what Ron Paul stands for: http://www.lewrockwell.com/paul/paul-arch.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---