简体   繁体   English

Python Flask-如何将值从一种途径传递到另一种途径?

[英]Python Flask - How to pass values from one route to another?

Hi I am new to flask and I am trying to create a simple login functionality. 嗨,我是flask的新手,我正在尝试创建一个简单的登录功能。 Users fill out their username and password (which at this point needs to match the username and password I hardcoded) and if their credentials are approved they are taken to their profile page. 用户填写用户名和密码(这时需要与我硬编码的用户名和密码匹配),如果他们的凭据被批准,则会被带到个人资料页面。 The profile page should show the message Hello followed by the username. 配置文件页面应显示消息Hello,后跟用户名。 The validation is working just fine and the user is taken to the profile page but I can't pass the username from the form (login.html) to the template "profile.html". 验证工作正常,用户被带到个人资料页面,但是我无法将用户名从表单(login.html)传递到模板“ profile.html”。 Below follows the code. 下面是代码。 I am sending the code that works but there is no tentative to pass the username. 我正在发送有效的代码,但没有通过用户名的尝试。

Thank you! 谢谢!

from flask import *

SECRET_KEY = "super secret"

app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
def index():
return render_template('login.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] == 'user' and request.form['password'] == 'pass':
            session['loggedin'] = True
            return redirect(url_for('profile'))
        else:
            error="Invalid credentials. Please try again."
    return render_template('login.html', error=error)

@app.route('/profile')
def profile():
    return render_template('profile.html')

@app.route('/logout')
def logout():
    session.pop('loggedin', None)
    return redirect(url_for('login'))



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

I think you miss the point of your hard work login page. 我认为您错过了辛勤工作登录页面的重点。

What about the next page the user will choose to visit? 用户将选择访问的下一页如何? Will you send the username value again? 您会再次发送用户名值吗? of course not.. 当然不是..

I suggest you to define a global var(session? DB data?) that contain the current-logged-in-user-data, so you can use all user's data, not only his username(age?posts? etc..) 我建议您定义一个包含当前登录用户数据的全局变量(会话?数据库数据?),这样您就可以使用所有用户的数据,而不仅仅是他的用户名(年龄?职位?等)。

One last thing, i use flask-login , i really like it, it simple mange my login session/views and guess what? 最后一件事,我使用flask-login ,我真的很喜欢它,它很简单地管理我的登录会话/视图并猜测是什么? there is current_user with the current-logged-in-user-data :) current_user和current-logged-in-user-data :)

Flask-login summery: 烧瓶登录总结:

Flask-Login provides user session management for Flask. Flask-Login提供Flask的用户会话管理。

It handles the common tasks of logging in, logging out, and remembering your users' sessions over extended periods of time. 它处理登录,注销和记住用户会话的常见任务。

Why not make use of flask's many useful modules? 为什么不利用flask的许多有用模块呢? They make flask an attractive microframework for speedy web development. 它们使烧瓶成为快速进行Web开发的有吸引力的微框架。

Flask-login, as stated above, streamlines authentication processes and manages sessions. 如上所述,Flask登录简化了身份验证过程并管理会话。 Flask sessions also automatically stores session data for logged-in users. Flask会话还自动为登录用户存储会话数据。 This allows you to implement a "Remember Me" feature in your login form. 这使您可以在登录表单中实现“记住我”功能。

Also, for security purposes, you would want to decorate some of your functions with @login_required , which is part of the flask-login module. 另外,出于安全性考虑,您可能希望使用@login_required装饰您的某些功能,该功能是flask-login模块的一部分。 This makes sure that the user is automatically redirected to the login page if he or she is not logged in already. 这样可以确保用户(如果尚未登录)将自动重定向到登录页面。

Here is an example of an index function that implements this: 这是实现此目的的索引函数的示例:

from flask import render_template, session
from flask.ext.login import login_required

@app.route('/')
@login_required
def index():
    return render_template("index.html")

You could also use flask.ext.openid to make authentication even more convenient. 您也可以使用flask.ext.openid使身份验证更加方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Altair 图从一个 Flask 路由传递到另一个? - How to pass an Altair graph from one Flask route to another? Flask Python - 如何将变量从一种方法传递到另一种方法 - Flask Python - How to pass variable from one method to another 将 session 中的变量传递到另一条路线 [Python FLASK] - Pass variable in a session into another route [Python FLASK] 如何将变量传递到FLASK中的另一条路线上 - How to pass variable onto another route in FLASK 如何在 flask 中传递值以将重定向(url_for())从一种方法返回到另一种方法 - How to pass values to return redirect(url_for()) from one method to another in flask 通过Flask(Python)将值从一个html页面传递到另一个页面 - Passing values from one html page to another via Flask (Python) 烧瓶:将参数传递给另一条路线 - Flask: pass argument to another route 我们如何在 Flask 中使用参数从另一条路由调用一条路由? - How can we call one route from another route with parameters in Flask? Python Flask 从另一个带有参数的路由调用路由 - Python Flask call route from another route with parameters Calling one function from another function or route using flask, python and sqlite - Calling one function from another function or route using flask, python and sqlite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM