赞
踩
这里作者使用的是conda进行安装,conda集成了很多的python包,且较容易管理安装包,所以使用conda比较舒适。conda安装可以参考其他博主的文章。
使用命令conda install flask
1.前端需要一个登录界面,用户名和密码的输入框以及登录按钮
2.后端需要处理前端请求,并完成成功的相应跳转
需求非常简单,接下来就是具体的实现环节。
登录界面是一个简单的表单,采用post请求。对于web基础知识不够理解的朋友,可以初步去了解一下。主要是http协议、html等内容的一个入门。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>登录</title>
- </head>
- <body>
- <form action="/login" method="post">
- <p>用户名:<input type="text" name="Username"></p>
- <p>密码:<input type="password" name="Passwd"></p>
- <input type="submit" value="登录">
- </form>
- </body>
- </html>
{{username}}是一个模板语法,可以获取到后端返回参数的值。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>欢迎</title>
- </head>
- <body>
- <h1>欢迎{{username}}</h1>
- <a href="/logout">登出</a>
- </body>
- </html>
登录的时候需要保存用户的session以便于再次访问时可以直接进入想要的反馈界面。同时登出的时候把session删除。session的具体知识细节可以参考其他博主的文章。
- #引入包
- from flask import Flask,request,session,render_template,redirect,url_for
-
- #创建对象
- app=Flask(__name__)
- app.secret_key="zqh"
- #定义登录界面
- @app.route("/")
- def index():
- return render_template("home.html")
-
- #处理登录请求
- @app.route("/login",methods=["POST","GET"])
- def login():
- #get请求重定向要home页面
- if request.method=="GET":
- #session没有就返回登录
- if session.get("Username"):
- return render_template("welcom.html", username=session["Username"])
- #如果有直接进入主页
- else:
- return redirect(url_for("index"))
- #post请求处理form表单
- else:
- #输入的不是空的用户名或空的密码
- if request.form["Username"]!="" and request.form["Passwd"]!="":
- session["Username"]=request.form["Username"]
- return render_template("welcom.html",username=session["Username"])
- #否则重新进行登录
- else:
- return redirect(url_for("index"))
- #处理登出
- @app.route("/logout")
- def logout():
- session.pop("Username",None)
- return redirect(url_for("index"))
- if __name__=="__main__":
- app.run(debug=True)

那么以上就是整个一个项目的搭建,比较简陋。完善的地方有很多,包括UI,以及用户名密码的验证,同时可以加入数据库的技术,使得整个项目实现完整的前后端分离,有视图层,有数据模型,有控制层。作者也在学习中,后期会更新相关的项目。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。