泰安公司网站开发,自己开外销网站怎么做,临时网页生成,济南传承网站建设公司毕业那会使用过这个轻量级的框架#xff0c;最近再来回看一下#xff0c;依赖相关的就不多说了#xff0c;直接从例子开始。下面示例中的 html 模板#xff0c;千万记得要放到 templates 目录下。 Flask基础示例 
hello world 
from flask import Flask, jsonify, url_fora…毕业那会使用过这个轻量级的框架最近再来回看一下依赖相关的就不多说了直接从例子开始。下面示例中的 html 模板千万记得要放到 templates 目录下。 Flask基础示例 
hello world 
from flask import Flask, jsonify, url_forapp  Flask(__name__)app.route(/)
def hello_world():return h1Hello World!/h1if __name__  __main__:app.run()路由 
既然涉及到web框架就必然涉及到路由。 
动态路由 
动态路由就是将变量拼接到路由 url 当中可以把字段编辑为variable_name这个部分将会作为命名参数传递到你的函数。如果在动态路由中指定了变量的类型比如 int:user_id则需要按照指定类型进行传值否则的话也会报错。参数也可以根据类似 request.args.get(id) 进行获取。 
from flask import Flask, requestapp  Flask(__name__)app.route(/)
def hello_world():return Hello World!app.route(/user/username)
def show_user_profile(username):id  request.args.get(id)return User %s, id %s % (username, id)app.route(/users/int:user_id)
def show_user_id(user_id):return User id %d % user_idif __name__  __main__:app.run(debugTrue)构造 url 
Flask 还可以用 url_for() 函数来给指定的函数构造URL也称为反向路由。它接收函数名作为第一个参数也接受对应 URL 规则的变量部分的命名参数。未知变量部分会添加到URL末尾作为查询条件。 
from flask import Flask, url_forapp  Flask(__name__)app.route(/)
def hello_world():return url_for(article, id1, namelook)  # /article/1?namelookapp.route(/article/id)
def article(id):return fid {id} article detailif __name__  __main__:app.run()http方法 
HTTP 有许多的不同的构造方法访问 URL 方法。默认情况下路由只回应 GET 请求当时通过route() 装饰器传递 methods 参数可以改变这个行为至于每个 method 对应的行为这块就不多细说了。 
from flask import Flask, url_for, requestapp  Flask(__name__)app.route(/, methods[GET, POST])
def index():if request.method  POST:return go post methodelif request.method  GET:return go get methodif __name__  __main__:app.run()模板 
html模板文件一般默认是放在 templates 目录下的如果没有这个目录的话可以自己新建一个。 
templates/index.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8titletemplate/title
/head
body
h1hello world!/h1
/body
/html 
from flask import Flask, render_templateapp  Flask(__name__)app.route(/)
def hello_world():return render_template(index.html)if __name__  __main__:app.run()变量替换 
而且还可以将内容传递到模板文件进行展示下面这种也是展示 hello world只不过我们将静态文件的内容藏起来了通过后端返回的内容再显示出来用的是模板语法两种方法在前端显示的都一样。 
!DOCTYPE html
html langen
headmeta charsetUTF-8titletemplate/title
/head
body
h1{{content}}/h1
/body
/html 
from flask import Flask, render_templateapp  Flask(__name__)app.route(/)
def hello_world():content  hello world!return render_template(index.html, contentcontent)if __name__  __main__:app.run()当然也可以将对象实例传递到模板中去。 
user_index.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleuser/title
/head
body
h1hello {{user.name}}/h1
/body
/html 
models.py 
class User:def __init__(self, id, name):self.id  idself.name  namemain.py 
from flask import Flask, render_template
from models import Userapp  Flask(__name__)# 引用模板
app.route(/)
def hello_world():content  hello world!return render_template(index.html, contentcontent)app.route(/user)
def user_index():user  User(1, Looking)return render_template(user_index.html, useruser)if __name__  __main__:app.run()条件语句  
info.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8title条件语句/title
/head
body{% if user.id  1 %}h1 Hello {{user.name}}/h1{% else %}h1There is no user!/h1{% endif %}
/body
/html 
from flask import Flask, render_template
from models import Userapp  Flask(__name__)# 路由
app.route(/info/user_id)
def info_judge(user_id):user  Noneif int(user_id)  1:user  User(1, Looking)return render_template(info.html, useruser)if __name__  __main__:app.run()循环语句 
list.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8title循环语句/title
/head
body{% for user in users %}h4user_id: {{user.id}}; user_name: {{user.name}}/h4br{% endfor %}
/body
/html 
from flask import Flask, render_template
from models import Userapp  Flask(__name__)# 路由
app.route(/list)
def info_judge():users  []for i in range(5):users.append(User(i, fstudent{i}))return render_template(list.html, usersusers)if __name__  __main__:app.run()模板继承 
我们会发现有一些网页的有些部分是不变的比如说页头页脚等当跳转相同网页的时候只有中间部分会改变这就要使用到模板的继承可以使用 extends 实现对模板文件的继承。 
base.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8title模板的继承/title
/head
bodydivH2Header 欢迎光临/H2/div{% block content %}{% endblock %}divH2Footer 欢迎下次再来/H2/div
/body
/html 
page_one.html 
{% extends base.html%}
{% block content %}h3{{content}}/h3
{% endblock %} page_two.html 
{% extends base.html%}
{% block content %}h3{{content}}/h3
{% endblock %} main.py 
from flask import Flask, render_template
app  Flask(__name__)# 第一页路由
app.route(/page_one)
def one_page():content  这是第一页return render_template(page_one.html, contentcontent)# 第二页路由
app.route(/page_two)
def secend_page():content  这是第二页return render_template(page_two.html, contentcontent)# 运行
if __name__  __main__:app.run() 消息提示 
使用 flash 可以将后台处理的消息提示刷新到页面 
index.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleFlask消息提示与异常捕获/title
/head
body
h1Login/h1form action/login methodpostinput typetext nameusername placeholder账号br /input typepassword namepassword placeholder密码 stylemargin-top:10pxbr /input typesubmit valueSubmit stylemargin-left:50px;margin-top:10px/form!--这里获取的是一个数组--{{get_flashed_messages()[0]}}
/body
/html 
main.py  
from flask import Flask, render_template, flash, requestapp  Flask(__name__)
# 对flash的内容加密
app.secret_key  123app.route(/login)
def index():return render_template(index.html)# 路由
app.route(/login, methods[POST])
def login():# 获取表单上传的数据form  request.formusername  form.get(username)password  form.get(password)# 进行判断if not username:flash(please enter username)return render_template(index.html)if not password:flash(please enter password)return render_template(index.html)if username  looking and password  123456:flash(login success)return render_template(index.html)else:flash(username and password not match)return render_template(index.html)# 运行
if __name__  __main__:app.run()异常捕获  
如果用户输入了错误的路径创建网站的人又没有设置异常捕获及处理它会出现404如果处理了的话那就显示的为处理后的页面。所以我们的异常处理也就是对返回的 404 页面或者其他异常返回我们设置的页面。 
404.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleFlask异常捕获与处理/title
/head
bodyh2抱歉你访问的页面去火星了....../h2br /h2请检查你的网址是否输入正确/h2
/body
/html 
user.html 
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
h1This is user page/h1
/body
/html main.py 
from flask import Flask, render_template, abortapp  Flask(__name__)# 异常捕获一
app.errorhandler(404)
def not_found():return render_template(404.html), 404# 异常捕获二
app.route(/user/user_id)
def user_info(user_id):if int(user_id)  1:return render_template(user.html)else:abort(404)if __name__  __main__:app.run()如果没有添加针对 404 的错误处理就是下面这种界面。 
403 是 Forbidden  文章转载自: http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn