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, 2019 in Python by krrish
• 160 points

edited Mar 12, 2019 by Omkar 1,022 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, 2019 by Omkar
• 69,220 points
0 votes
Well thank you, I found a way from your comments
answered Jan 19, 2019 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 aryya
• 7,460 points
697 views
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,443 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
0 votes
1 answer

How to solve indentation in the below code?

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

answered Feb 9, 2019 in Python by Omkar
• 69,220 points
680 views
0 votes
1 answer

Need help understanding python code

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

answered Feb 19, 2019 in Python by Omkar
• 69,220 points
525 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP