oTree-org / oTree

Python framework for multiplayer decision games, behavioral experiments, and surveys
https://groups.google.com/g/otree
Other
464 stars 199 forks source link

session has no attribute ***It doesn't seem to recognize `foo` as `1` #83

Closed zangguojun closed 4 years ago

zangguojun commented 4 years ago

models.py -->Group Constants.num_rounds == 10

        if self.round_number == Constants.num_rounds:
            for player in players:
                self.session.vars[player.id_in_group] = {}
                rounds = player.in_all_rounds()
                for index,round in enumerate(rounds,start=1):
                    self.session.vars[player.id_in_group][index] = [round.deposit, round.round_deposit]
                    from pprint import pprint
                    pprint(self.session.vars)
            print('*'*10)

------------------------> output:

{1: {1: [3, 18.2],
     2: [0, 20.0],
     3: [0, 20.0],
     4: [0, 20.0],
     5: [0, 20.0],
     6: [0, 20.0],
     7: [0, 20.0],
     8: [0, 20.0],
     9: [0, 20.0],
     10: [0, 20.0]},
 2: {1: [0, 21.2],
     2: [0, 20.0],
     3: [0, 20.0],
     4: [0, 20.0],
     5: [0, 20.0],
     6: [0, 20.0],
     7: [0, 20.0],
     8: [0, 20.0],
     9: [0, 20.0],
     10: [0, 20.0]},
 3: {1: [0, 21.2],
     2: [0, 20.0],
     3: [0, 20.0],
     4: [0, 20.0],
     5: [0, 20.0],
     6: [0, 20.0],
     7: [0, 20.0],
     8: [0, 20.0],
     9: [0, 20.0],
     10: [0, 20.0]},
 4: {1: [0, 21.2],
     2: [0, 20.0],
     3: [0, 20.0],
     4: [0, 20.0],
     5: [0, 20.0],
     6: [0, 20.0],
     7: [0, 20.0],
     8: [0, 20.0],
     9: [0, 20.0],
     10: [0, 20.0]},
 5: {1: [0, 21.2],
     2: [0, 20.0],
     3: [0, 20.0],
     4: [0, 20.0],
     5: [0, 20.0],
     6: [0, 20.0],
     7: [0, 20.0],
     8: [0, 20.0],
     9: [0, 20.0],
     10: [0, 20.0]},
 'manager': 5,
 'nums': [1, 2, 3, 4, 5]}

In one test page: I want do this,

          {% for foo in session.vars.1 %}
                {{ foo }}    ## here correct ! !  {{foo}} is 1,2,3...
                {{ session.vars.1.foo }}  ## here  ERROR
                <br>
            {% endfor %}
              {{session.vars.1.1}}   ## here correct ! ! 

but error **session has no attribute "vars.1.foo"** It doesn't seem to recognize foo as 1 What should I do?? Looking forward to hearing from you and thanks!

oTree-org commented 4 years ago

That is how the django template system works. Otherwise it would be ambiguous. What if you wanted to access a key called “foo”?

Sent from my phone

On Nov 18, 2020, at 9:30 AM, zangguojun notifications@github.com wrote:

 Reopened #83.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

zangguojun commented 4 years ago

I want to traverse session.vars.['1'], session.vars.['2'], session.vars.['3'] and so on in session.vars rather than write session.vars.['1']session.vars.['2']session.vars.['3'] directly.This is too much trouble.

oTree-org commented 4 years ago

If you want to iterate over keys and values of a dict, use the .items method. But anyway I think you will have an easier time if you use vars_for_template to send just the data you need to the template.

Sent from my phone

On Nov 18, 2020, at 10:20 AM, zangguojun notifications@github.com wrote:

 I want to traverse session.vars.['1'], session.vars.['2'], session.vars.['3'] and so on in session.vars rather than write session.vars.['1'],session.vars.['2'],session.vars.['3'] directly.This is too much trouble.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

zangguojun commented 4 years ago

If you want to iterate over keys and values of a dict, use the .items method. But anyway I think you will have an easier time if you use vars_for_template to send just the data you need to the template.

Sent from my phone

On Nov 18, 2020, at 10:20 AM, zangguojun notifications@github.com wrote:

 I want to traverse session.vars.['1'], session.vars.['2'], session.vars.['3'] and so on in session.vars rather than write session.vars.['1'],session.vars.['2'],session.vars.['3'] directly.This is too much trouble.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

Thanks! But I need to pass data between APPS, which should be the best way to do it.