python: server is not receiving input from client flask

Jerry Thefilmmaker jerrythefilmmaker at gmail.com
Tue Jun 29 16:07:42 EDT 2021


On Tuesday, June 29, 2021 at 2:03:58 PM UTC-4, Chris Angelico wrote:
> On Wed, Jun 30, 2021 at 3:38 AM Jerry Thefilmmaker 
> <jerrythe... at gmail.com> wrote: 
> > Thanks for taking the time to explained, Chris. Believe me, it helps. It had me thinking for a while. So, All the stuff about HTTP makes sense. I've been studying and trying to wrap my head around the rest of your explanation and see how I can apply it to my codes. 
> >
> Glad to hear it :)
> > So, if I separate my GET method which would be to get the web page from my POST method which would be assigned to returning value from my form, wouldn't I have to dedicate 2 functions for any given requests in that sense? 
> >
> The web page and form response are two completely separate requests. 
> You can't think of them as a single request; in fact, it's entirely 
> possible for someone to send the POST request without first getting 
> the page at all.
> > The only reason I have parameters in my functions is so I can pass value from 1 function to another which is just stuff that happens in the background. Is it necessary to have them in my decorators as well, or is it that these values won't get passed around if they're not integrated in the decorators? 
> >
> They won't get passed around. The only way to share that information 
> is via the page itself (or something along those lines, like a session 
> variable - which is some magic built on top of cookies). Think about 
> the two requests completely independently, and make sure that (a) the 
> GET request sends back the correct page, and (b) the POST request does 
> whatever it needs to, and sends back a new page. 
> 
> Hopefully that should set you in the right direction! 
> 
> ChrisA

Got it. It all makes sense. It feels like I'm doing all the right things, accounting for all functions but somehow it's not working.

I changed my methods. Adjusted my routes. Still the same thing. Now I get this error:
      i3 = ans.get(answers, '')
AttributeError: 'str' object has no attribute 'get'

Which doesn't make sense because my object ans is a dictionary and get method here is supposed to get another value within the dictionary based on the key value of answers which is passed from the client. Does that make sense?

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Here's my modified code by the way:

@app.route("/")
def main():
    return render_template('app.html')

@app.route("/play", methods = ['GET']) 
def play():

    qa = questions.qa #list of dict with questions
    #user1, user2 = [], [] #containers for each player's answers

    if request.method == 'GET':
        answers = request.form.get('answers')
        random.shuffle(questions.qa)
        response = quiz(qa)
    print(answers)
    return response 

def quiz(qa):
    for rand_q in qa:
        i = rand_q['question']
        i2 = check_answer(i)
       
    return render_template('app.html') + i, i2 

@app.route("/check_answer/<ans>", methods = ['POST'])
def check_answer(ans):
    user1 = []
    if request.method == 'POST':
        answers = request.form.get('answers')
        if 'yes' in answers:
            print(ans)
            user1.append('yes')
            i3 = ans.get(answers, '')
        return i3
----------------------------------------------------------------------------------------------------------------------------------------------------------
Here's my form from the html file:

<form action="check_answer/ans" method="POST">
			Enter your answer: <input type="text" name="answers">
			 <input type="submit" value="submit">
	</form>

Also, where do I see my print statement in this case? Mind you the codes worked fine before I attempted to use flask.


More information about the Python-list mailing list