Hey guys I've been working on my final project. I keep running into this error When I run flask, this error occurs
File "/home/ubuntu/project/app.py", line 88, in login
if request.method == "GET":
AttributeError: 'function' object has no attribute 'method'
It points towards this line
if request.method == "GET":
I don't understand why it doesn't work. A friendly soul on the CS50 discord has pointed me towards this stack overflow link https://stackoverflow.com/questions/46520034/attributeerror-function-object-has-no-attribute-method-for-if-request-metho but I don't understand what "flask.request is LocalProxy unbound but requests.request is a function" means. I would appreciate a lot if anybody could help me with this. Here's my code in case it helps to diagnose the issue.
import os
from flask import Flask, flash, redirect, render_template, request, session
from flask import request
from flask_session import Session
import sqlite3
from cs50 import SQL
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash
from datetime import datetime
from functools import wraps
# Configure application
app = Flask(__name__)
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
db = SQL("sqlite:///database.db")
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
if request.method == "GET":
return render_template("login.html")
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
session.clear()
# Ensure username was submitted
if not request.form.get("username"):
return("Apologies, no username was inputted")
# Ensure password was submitted
elif not request.form.get("password"):
return("Apologies, no password was inputted")
if request.form.get("accountType") == "Ward":
# Query database for username
rows = db.execute("SELECT * FROM ward_info WHERE username = ?", request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/")
if request.form.get("accountType") == "Guardian":
# Query database for username
rows = db.execute("SELECT * FROM guardian_info WHERE username = ?", request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/")
Thanks!
if request.method == "GET":
is line 53 and thisif len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
is line 88.