In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Python Flask to achieve login function". In daily operation, I believe many people have doubts about how to use Python Flask to achieve login function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Python Flask to achieve login function". Next, please follow the editor to study!
0x01 routing from flask import Flaskapp = Flask (_ _ name__) # flask object instantiation @ app.route ('/ index') # Definitions Home @ app.route ('/') # set default indexdef index (): return 'hello worldview objective instantiation app.route (' / home/') # generate home route Single parameter def home (username): print (username) return 'Welcome home' @ app.route ('/ main//') # multiple parameters pass def main (username,password): print (username) print (password) return 'welcome'def about (): return' about page'app.add_url_rule (rule='/about') View_func=about) # another way to add routes if _ _ name__ = ='_ main__': app.debug = True # Open debug mode app.run () 0x02 template and static file 2.1 file structure 2.2 code # app.py#app.pyfrom flask import Flask Render_template # pour template app = Flask (_ _ name__) # declare template folder @ app.route (('/ index')) def index (): return render_template ('index.html') # return template if _ _ name__ = =' _ main__': app.run (debug=True) Title hello hello
0x03 jsonfrom flask import Flask,jsonifyapp = Flask (_ _ name__) @ app.route ('/') def index (): user = {'name':' Li San', 'password':'123'} return jsonify (user) if _ _ name__ =' _ _ main__': app.run (debug=True) 0x04 Redirect 4.1 access Redirect from flask import Flask Redirect # Import jump module app = Flask (_ _ name__) @ app.route ('/ index') def index (): return redirect ('https://www.baidu.com') # specify the jump path Visit the / index directory and jump to the home page of Baidu @ app.route ('/ home') def home (): return 'home page'if _ _ name__ =' _ _ main__': app.run (debug=True) 4.2 print routing from flask import Flask Url_for # Import module app = Flask (_ _ name__) @ app.route ('/ index') def index (): return 'test'@app.route (' / home') def home (): print (url_for ('index')) print index routing return' home page'if _ name__ = ='_ main__': app.run (debug=True) 4.3 Hop transfer Parameter # access home Bring name into index and display it on the page from flask import Flask,url_for,redirect # Import module app = Flask (_ _ name__) @ app.route ('/ index') def index (name): return 'test% s'% name@app.route ('/ home') def home (): return redirect (url_for ('index') Name='admin')) if _ _ name__ = ='_ main__': app.run (debug=True) 0x05 jinjia2 template 5.1Code from flask import Flask,render_template # pour the template app = Flask (_ _ name__) # declare the template folder @ app.route (('/ index')) def index (): user = 'admin' data = [' 111Zhen2Jing'Li San'] userinfo = {'username':'lisan' 'password':'12333'} return render_template (' index.html',user=user,data=data,userinfo=userinfo) # returns the template Incoming data if _ _ name__ = ='_ main__': app.run (debug=True) Title 11111 {{user}} {{data}} # pass {% if user = = 'admin'%} # simple logic judgment administrator {% else%} regular user {% endif%} {% for item in data%} # for loop {{item}} { % endfor%} {{userinfo ['username']}} {{userinfo [' password']}} {{user | upper}} # uppercase (for more information on jinjia2 filters) 0x06 blueprints
The purpose is to subdivide the functional modules better.
Code structure ├── admin │ └── admin.py └── app.py6.2 Code # admin.pyfrom flask import Blueprint Import Blueprint Module admin = Blueprint ('admin',__name__,url_prefix='/admin') # object instantiation, url_prefix adds a routing prefix, indicating that if you want to access the relevant routes on this page, you can only access them through something like xxx/admin/login Cannot xxx/login access @ admin.route ('/ register') def register (): return 'Welcome Registration' @ admin.route ('/ login') def login (): return 'Welcome login' # app.pyfrom flask import Flaskfrom admin.admin import admin as admin_blueprint # Import Blueprint app = Flask (_ name__) # declare template folder app.register_blueprint (admin_blueprint) # Registration Blueprint @ app.route ((') / index')) def index (): return 'index page'if _ _ name__ = =' _ main__': app.run (debug=True) 0x07 login 7.1 structure 7.2Code # web.pyfrom flask import Flask Render_template,request,redirect,flash,url_for,sessionfrom os import urandomapp = Flask (_ _ name__) app.config ['SECRET_KEY'] = urandom (50) @ app.route (' / index') def index (): if not session.get ('user'): flash (' Action after login', 'warning') return redirect (url_for (' login')) return render_template ('index.html') @ app.route (' / login') Methods= ['GET','POST']) def login (): if request.method = =' GET': return render_template ('login.html') elif request.method = =' POST': username = request.form.get ('username') password = request.form.get (' password') if username = 'admin' and password = =' 88888888: flash ('login successful' 'success') session [' user'] = 'admin' return redirect (url_for (' index')) else: flash ('login failed', 'danger') return redirect (url_for (' login')) if _ _ name__ = ='_ main__': app.run (debug=True) # index.html Title Welcome Administrator {% for color, message in get_flashed_messages (with_categories=True)%} ×
{{message}}
{% endfor%} # login.html login {% for color, message in get_flashed_messages (with_categories=True)%} ×
{{message}}
{% endfor%} at this point, the study on "how to use Python Flask to achieve login function" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.