简体   繁体   中英

'request' object not getting imported from flask module

I'm unable to import the 'request' function from flask, it is facing some error or something I guess. Is there any solution?

app.py

from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)


    def __repr__(self):
        return '<Task %r>' %self.id

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        pass
    else:
        return render_template('index.html')

    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

The request function is instead getting turned to a variable

Also I am facing the same kind of error in another code while trying to import current_user from flask_login

Is there any solution for it? or alternate way out?

What you're seeing, if you dig deeper, is that request is an instance of LocalProxy , which is a bit of behind-the-scenes magic that ensures that what might appear to be a global variable gets the value of the specific request that invoked the handler.

What matters is what happens when you run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM