Help me solve the code

0 votes

Hello everyone, I train to create a website with Flask and Python. I have setup db and this is the code for my login information:

@app.route('/login/', methods=["POST","GET"])
def loginpage():
message=""
c, conn = connection()
try:
if request.method=="GET":
return render_template("login.html",message=message)
if request.method=="POST":
data=c.execute("SELECT * FROM user WHERE username = '%s'"%(request.form['username']) )
data=c.fetchone()[2]

if data==request.form['password']:
message = "Success"
session['loged-in']=True
session['username']="flag{lollellul}"
return rendertemplate("login.html",message=message) else : message ="Unknown user" return rendertemplate("login.html",message=message)
except Exception as e:
message=str(e)
return render_template("login.html", message=message)

But when I test it, it always returns the error. The 'NoType' object does not have the 'getitem' attribute I edited, but it is still not better
Can anybody help me with it to get?

Jan 9 in Python by krrish
• 160 points

edited Mar 12 by Omkar 59 views

Is this the complete code? The error says:

The 'NoType' object does not have the 'getitem' attribute

But I can not see getitem being used anywhere in the code.  

2 answers to this question.

+1 vote

The fetchone() method fetches the result of the query one row at a time. When there are no more rows to fetch, it retrieves None. So, while implementing a fetchone() method, it is suggested to use an if condition right after fetching. Try this:

c.execute("SELECT * FROM user WHERE username = '%s'"%(request.form['username']) )
fetch_result=c.fetchone()
if fetch_result:    
  data = fetch_result[2]
    #Here goes rest of the logic
answered Jan 9 by Omkar
• 67,380 points
0 votes
Well thank you, I found a way from your comments
answered Jan 19 by krrish
• 160 points
Glad it helped :)

Related Questions In Python

0 votes
1 answer

Can you help me understand the Global Interpreter Lock in Python

Suppose you have multiple threads which don't really touch ...READ MORE

answered Nov 23, 2018 in Python by ariaholic
• 7,340 points
50 views
0 votes
1 answer
0 votes
1 answer

Need help writing a dataframe into a csv with the help of a loop

Using the following logic you can arrive ...READ MORE

answered Apr 17, 2018 in Python by anonymous
933 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 220 views
+4 votes
6 answers
0 votes
1 answer

How to solve indentation in the below code?

Python does not use brackets, it uses ...READ MORE

answered Feb 9 in Python by Omkar
• 67,380 points
15 views
0 votes
1 answer

Need help understanding python code

In this particular code, I think you ...READ MORE

answered Feb 19 in Python by Omkar
• 67,380 points
40 views