Python 为什么我的登录函数只是重定向,而没有执行func中所写的操作
我花了几个小时,但我不知道哪里出了错。 我正在尝试flask,希望实现一个登录页面。 但是登录不起作用,只是转发而没有检查,我做错了什么 这是登录功能:Python 为什么我的登录函数只是重定向,而没有执行func中所写的操作,python,flask,flask-login,Python,Flask,Flask Login,我花了几个小时,但我不知道哪里出了错。 我正在尝试flask,希望实现一个登录页面。 但是登录不起作用,只是转发而没有检查,我做错了什么 这是登录功能: @app.route('/login', methods=['GET', 'POST']) def login(): name = request.form.get('name') password = request.form.get('password') remember = True if request.form.get('remem
@app.route('/login', methods=['GET', 'POST'])
def login():
name = request.form.get('name')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(name=name).first()
if user is None or not check_password_hash(user.password, password):
flash('Please check your login details and try again.')
return redirect(url_for('auth.login'))
else:
return redirect(url_for('profile.profile'))
return render_template('login.html')
每当我试图访问“http://localhost../login“它只是重定向到/profile,当登录成功时,我只想重定向到/profile。
我几乎花了一整天的时间在互联网上寻找解决方案,并尝试了很多东西,但都不起作用
以下是我的应用程序的层次结构:
├── app
│ ├── app.db
│ ├── auth
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-39.pyc
│ │ │ └── views.cpython-39.pyc
│ │ └── views.py
│ ├── base
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-39.pyc
│ │ │ └── views.cpython-39.pyc
│ │ └── views.py
│ ├── chat
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-39.pyc
│ │ │ └── views.cpython-39.pyc
│ │ └── views.py
│ ├── home
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-39.pyc
│ │ │ └── views.cpython-39.pyc
│ │ └── views.py
│ ├── __init__.py
│ ├── models.py
│ ├── profile
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-39.pyc
│ │ │ └── views.cpython-39.pyc
│ │ └── views.py
│ ├── __pycache__
│ │ ├── auth.cpython-39.pyc
│ │ ├── __init__.cpython-39.pyc
│ │ └── models.cpython-39.pyc
│ ├── static
│ │ ├── base.css
│ │ ├── home.css
│ │ ├── login.css
│ │ └── main_bgr.jpg
│ └── templates
│ ├── base.html
│ ├── chat.html
│ ├── home.html
│ ├── login.html
│ ├── profile.html
│ └── signup.html
在第一个“如果”语句中,我还尝试了:
if user.name == None
但这会导致“页面没有正确重定向”我完全确定这是否可行,但我注意到,对于方法是POST还是GET没有条件
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == "POST" :
name = request.form.get('name')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(name=name).first()
if user is None or not check_password_hash(user.password, password):
flash('Please check your login details and try again.')
return redirect(url_for('auth.login'))
else:
return redirect(url_for('profile.profile'))
return render_template('login.html')
现在,如果这不能解决问题,我再次表示歉意。我将其更改为:
@app.route('/login', methods=['GET', 'POST'])
def login():
''' Login Method '''
if request.method == 'POST':
print('post')
name = request.form.get('name')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
# = User(name=name, password=password)
user = User.query.filter_by(name=name).first()
if not user or not check_password_hash(user.password, password):
return render_template('login.html')#redirect(url_for('auth.login'))
else:
return render_template('login.html')
return redirect(url_for('home.home'))
现在它开始工作了!:)非常感谢你@Vishnu Joshi,你的方法帮助我修复了它 分配后打印
用户
值it@AriaN这是print的输出:“User None”尝试用==
替换is
,看看它是否已修复not@AriaN不,还是一样。检查len(user)==0
No,它不起作用,实际上它只是发出GET请求,但没有POST请求很高兴知道它有帮助!