oTree Forum >

Noself format for self.request.POST.get

#1 by AngelVJimenez

I am creating my first experiment with OTree. I am using OTree Studio with the noself format. 
In my experiment, participants have to choose between two potential partners to interact with. I want a function to return the id of the chosen partner. 
I think this would work in the self format. 

def before_next_page(self):
        chosen_partner_id = self.request.POST.get('chosen_partner')
        if chosen_partner_id:
            self.player.chosen_partner = int(chosen_partner_id)
Does somebody know how I can change it to the noself format? I have tried to remove “self” from the code with no luck. 
It returns the following error: 

AttributeError: 'Player' object has no attribute 'request'

Thank you very much. 



#2 by Daniel_Frey

Do you have an IntegerField (in the Player class) with that question and do you present this question to the participants? If yes, the Player's answer will be saved in that field.

#3 by AngelVJimenez

Hi Daniel, 

Thank you for your reply. 

Yes, I have an IntegerField in the Player class: 

chosen_partner = models.IntegerField()

And this question is presented to participants. 

However, it does not work. It gives me the AtributeError: "'Player' object has no attribute 'request'"

I am not able to adapt the self format to noself. 

This is the code I am using: 

chosen_partner_id = player.request.POST.get('chosen_partner')
if chosen_partner_id:player.chosen_partner = int(chosen_partner_id)

#4 by Daniel_Frey

I'm not sure why you use the request.GET method.

How does your HTML page containing this question look?

#5 by AngelVJimenez

I am not sure the regquest.GET method is correct either. 

This is the HTML page. Thanks

<p>Your potential partners and their reputations are:</p>



Player {{ potential_partner1.id_in_group }}: Reputation score = {{ potential_partner1.reputation }}



Player {{ potential_partner2.id_in_group }}: Reputation score = {{ potential_partner2.reputation }}

<button type="submit" name="chosen_partner" value="{{ potential_partner1.id_in_group }}">Choose Player {{ potential_partner1.id_in_group }}</button>
<button type="submit" name="chosen_partner" value="{{ potential_partner2.id_in_group }}">Choose Player {{ potential_partner2.id_in_group }}</button>
{{ formfields }}

{{ next_button }}

#6 by Daniel_Frey

I see the problem: I believe self.request.POST.get depends on Django, which is no longer supported.

You could use live Pages to get the values from the page to the database: https://otree.readthedocs.io/en/latest/live.html.

I tried out something like this; of course, you need to check that yourself:

In your Player class, add:

    def live_method(player: Player, data):
        player.chosen_partner = data
        print(player.chosen_partner)  # to check if the data was received
Then, you can add a live_method on the page (click on Advanced --> live_method, and then name the function live_method).

After that

Change your buttons like this:

<button type="button" id="btn1" onclick="sendValue_btn1()" value="{{ potential_partner1.id_in_group }}">Choose Player {{ potential_partner1.id_in_group }}</button>

<button type="button" id="btn2" onclick="sendValue_btn2()" value="{{ potential_partner2.id_in_group }}">Choose Player {{ potential_partner2.id_in_group }}</button>

And add a JavaScript section on the page:

    btn1 = document.getElementById("btn1");
    btn2 = document.getElementById("btn2");

    function sendValue_btn1() {
        console.log('in sending');

    function sendValue_btn2() {

I'm sure this could be achieved in a more thoughtful way, but in my test, it worked :)
I hope that helps!


#7 by Daniel_Frey

By the way, I changed the button type to type="button" so clicking it won't submit the page directly.

#8 by AngelVJimenez

Thank you, Daniel. This solves the problem. Now, it works :)



Write a reply

Set forum username