flask 第五篇
需求:
1. 用户名: oldboy 密码: oldboy123
2. 用户登录成功之后跳转到列表页面
3. 失败有消息提示,重新登录
4.点击学生名称之后,可以看到学生的详细信息
后端:
from flask import Flask
from flask import request
from flask import render_template
from flask import redirect USER = {'username': 'oldboy', 'password': "oldboy123"} STUDENT_DICT = {
: {'name': 'Old', 'age': , 'gender': '中'},
: {'name': 'Boy', 'age': , 'gender': '男'},
: {'name': 'EDU', 'age': , 'gender': '女'},
} app = Flask(__name__) @app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
if request.form["username"] == USER["username"] and request.form["password"] == USER["password"]:
return redirect("/student_list")
return render_template("login.html", msg="用户名密码错误") return render_template("login.html", msg=None) # 如果前端Jinja2模板中使用了msg,这里就算是传递None也要出现msg @app.route("/student_list")
def student():
return render_template("student_list.html", student=STUDENT_DICT) @app.route("/info")
def student_info():
stu_id = int(request.args["id"])
stu_info = STUDENT_DICT[stu_id]
return render_template("student.html", student=stu_info, stu_id=stu_id) app.run("0.0.0.0", , debug=True)
代码
前端:
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Old Boy EDU</title>
</head>
<body>
<form method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="登录">
{{ msg }}
</form>
</body>
</html>
登录页面代码
学生信息页面代码: student_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Old Boy EDU</title>
</head>
<body>
Welcome to Old Boy EDU
<table border="2xp">
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>option</td>
</tr>
</thead>
<tbody>
{% for foo in student %}
<tr>
<td>{{ foo }}</td>
<td>{{ student[foo].name }}</td>
<td><a href="/info?id={{ foo }}">详细</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
学生信息
学生信息代码 student.html 字典形式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Old Boy EDU</title>
</head>
<body>
Welcome to Old Boy EDU
<table border="1px">
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>gender</td>
</tr>
</thead>
<tbody>
<tr>
<td>{{ stu_id }}</td>
<td>{{ student.name }}</td>
<td>{{ student["age"] }}</td>
<td>{{ student.get("gender") }}</td>
</tr>
</tbody>
</table>
<div><a href="/student_list">返回</a></div>
</body>
</html>
字典形式学生信息
第五篇,完结
flask 第五篇的更多相关文章
- Flask最强攻略 - 跟DragonFire学Flask - 第五篇 做一个用户登录之后查看学员信息的小例子
需求: 1. 用户名: oldboy 密码: oldboy123 2. 用户登录成功之后跳转到列表页面 3. 失败有消息提示,重新登录 4.点击学生名称之后,可以看到学生的详细信息 后端: from ...
- Flask第五篇——设置debug模式
flask默认是没有开启debug模式的,开启debug模式有很多好处: 第一,可以帮助我们查找代码里面的错误,比如: # coding: utf-8 from flask import Flask ...
- 【Python五篇慢慢弹】快速上手学python
快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
- Python之路【第十五篇】:Web框架
Python之路[第十五篇]:Web框架 Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 1 2 3 4 5 6 ...
- 【Python五篇慢慢弹】数据结构看python
数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...
- 【Python五篇慢慢弹(3)】函数修行知python
函数修行知python 作者:白宁超 2016年10月9日21:51:52 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...
- 【Python五篇慢慢弹(4)】模块异常谈python
模块异常谈python 作者:白宁超 2016年10月10日12:08:31 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondo ...
- 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸
类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...
- 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)
解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...
随机推荐
- linux mint 安装xshell
之前在Windows上进行开发的时候,SSH重度依赖SecureCRT或者XShell工具,现在把办公环境迁移到Linux后,每次连接都需要输入密码,尤其是需要跳板机的时候,需要逐级输入,十分麻烦.所 ...
- PHP扩展之 Imagick安装
最近的PHP项目中,需要用到切图和缩图的效果,在本地windows开发环境,安装过程遇到好多问题,在此与大家分享. php官网里,一大群老外也看不懂这玩意怎么装,主要原因在于,php版本庞杂,还有x8 ...
- 解决'androidx.arch.core:core-runtime' has different version for the compile (2.0.0) and runtime (2.0.1)
先说原因,我们引用的包版本不同产生了冲突,所以编译不通过.解决的办法是在引用的时候排除一个版本,只留一个版本. 解决过程: 先找出哪些库引用了相同的库,仅仅是版本不同. gradle app:depe ...
- django orm 数据查询详解
一 在django里面创建模型 from django.db import models class Blog(models.Model): name = models.CharField(max_l ...
- 解决GitHub下载资源慢的问题
打开 C:\Windows\System32\drivers\etc\hosts 添加 # GitHub 解决下载速度慢的问题 192.30.253.113 github.com 151.101.18 ...
- 自己Linux东西存放情况
elasticsearch /home/panfeng 包含了 elasticsearch 和 ik分词器 FastDFS https://www.cnblogs.com/taopanfeng/p/1 ...
- sql 批量触发器
SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO ALTER TRIGGER [dbo].[tr_insert_update_delete_sscode] O ...
- -bash: ls: No such file or directory 错误的原因及解决办法
ubuntu出现如下错误: { Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-42-generic x86_64) * Documentation: ...
- npm run build后如何打开index.html跑起项目
Tip: built files are meant to be served over an HTTP server. Opening index.html over file:// won't ...
- Summer training #9
A:树形DP 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点 要改变的边的权值为1,不需要改变的边的权值为0, ...