为什么我的登录函数只是重定向而不执行 func 中写的内容
Why is my login function just redirecting without doing what is written in the func
我花了几个小时,但我不知道错误在哪里。
我现在正在尝试烧瓶,想实现一个登录页面。
但是登录不上去,不检查就直接转发,我哪里做错了?
这是登录函数:
@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,我只想在登录成功时重定向到这里。
我几乎整天都在网上寻找解决方案,尝试了很多方法,但都不行。
这是我的应用程序的层次结构:
├── 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”语句中我也尝试过:
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,您的方法帮助我解决了问题!
我花了几个小时,但我不知道错误在哪里。 我现在正在尝试烧瓶,想实现一个登录页面。 但是登录不上去,不检查就直接转发,我哪里做错了?
这是登录函数:
@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,我只想在登录成功时重定向到这里。 我几乎整天都在网上寻找解决方案,尝试了很多方法,但都不行。
这是我的应用程序的层次结构:
├── 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”语句中我也尝试过:
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,您的方法帮助我解决了问题!