网站开发 报刊,广州建网站自助建站系统,统计网站怎么做,广告设计与制作论文前言
flask基础
搭建flask服务器
定义html
使用templates模板定义页面的html
html页面编写
render_template传参变量
定义图片
创建static目录#xff0c;存入图片
html编写
flask入门
网站多域名
网站之间超链接跳转
入门案例
将centos的rpm包下载链接集成到自…
前言
flask基础
搭建flask服务器
定义html
使用templates模板定义页面的html
html页面编写
render_template传参变量
定义图片
创建static目录存入图片
html编写
flask入门
网站多域名
网站之间超链接跳转
入门案例
将centos的rpm包下载链接集成到自己的服务器
1.编写代码
2.导出html页面并放到template目录
3.编写Index.html
4.访问127.0.0.1:8080 前言 Flask诞生于2010年是Armin ronacher用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架。
特点 微框架、简洁、只做他需要做的给开发者提供了很大的扩展性。 Flask和相应的插件写得很好用起来很爽。 开发效率非常高比如使用SQLAlchemy的ORM操作数据库可以节省开发者大量书写sql的时间。 把默认的Jinija2模板引擎替换成其他模板引擎都是非常容易的。 框架对比 1 框架轻重
重量级的框架为方便业务程序的开发提供了丰富的工具、组件如Django 轻量级的框架只提供Web框架的核心功能自由、灵活、高度定制如Flask、Tornado 2与Django对比
django提供了
django-admin快速创建项目工程目录 manage.py 管理项目工程 orm模型数据库抽象层 admin后台管理站点 缓存机制 文件存储系统 用户认证系统 一些常用的 Flask 方法的简要介绍 route(rule, methods[GET]) 用于定义 URL 规则和请求方法的映射关系指定了响应请求的处理函数。 render_template(template_name_or_list, **context) 用于渲染模板文件并传递上下文变量到模板中进行渲染。 request.args 获取请求 URL 中的查询参数。 request.form 获取 POST 请求发送的表单数据。 request.files 获取上传的文件数据。 session 用于存储用户会话信息可以在不同请求之间共享数据。 redirect(location, code302) 重定向到指定的 URL。 url_for(endpoint, **values) 生成指定端点对应的 URL。 jsonify(*args, **kwargs) 将传入的数据序列化为 JSON 格式并创建包含 JSON 数据的 Response 对象。 make_response(*args) 创建一个自定义的响应对象可以设置 HTTP 状态码、头部信息等。 abort(status_code) 中止请求并返回指定的 HTTP 状态码。 flask基础
搭建flask服务器
from flask import Flaskwebapp Flask(__name__)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue) 导入 Flask 模块用于创建 Web 应用程序 创建了一个名为 webapp 的 Flask 应用实例 启动 Flask 应用程序监听本地主机的 127.0.0.1 地址的 8080 端口设置 debug 模式为 True出现错误时后台显示调试信息 截图显示有一个 GET 请求尝试访问根路径 /但是返回了 404 错误表明应用中没有处理根路径的路由 所以要添加url路由观察如下代码 from flask import Flaskwebapp Flask(__name__)webapp.route(/)
def index():return 这是一个测试页面if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue) 使用装饰器 webapp.route(/) 调用route路由括号里给定参数/符号默认为首页 当路由定位到这个默认的页面时就调用这个index函数的返回内容。 所以访问127.0.0.1:8080时实际上默认访问的是127.0.0.1:8080/ 这个/ 就根据路由走到了index函数 返回一个正常的页面了 正常来说访问一个页面的时候返回的是html的精美页面那么如何实现呢
定义html
使用templates模板定义页面的html
在当前项目中新建目录templates html页面编写
!DOCTYPE html
html langen
head
meta charsetUTF-8
title我的网页/title
/head
bodyh1欢迎来到我的网页/h1
p这是一个简单的 HTML 示例。/p/body
/html from flask import Flask
from flask import render_templatewebapp Flask(__name__)webapp.route(/)
def index():return render_template(index.html)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue) 导入render_template方法 调用该方法指定 index.html 因为flask框架在使用这个模板函数时默认去寻找项目文件夹下的templates文件夹里的html文件 render_template传参变量
如果想通过render_template方法传输数据在html文件中显示出来需要在render_template函数中加入数据参数如datamsg
html编写格式{{变量}}
from flask import Flask
from flask import render_templatewebapp Flask(__name__)webapp.route(/)
def index():msg Welcomereturn render_template(index.html, datamsg)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue) 加了变量msgrender_template方法中制定了data数据 html编写加上{{data}}调用变量内容
!DOCTYPE html
html langen
head
meta charsetUTF-8
title我的网页/title
/head
bodyh1欢迎来到我的网页/h1
p这是一个简单的 HTML 示例。/p
{{data}} # 显示传递变量的内容 Welcome
/body
/html 定义图片
如果想在该页面添加图片或者css、js文件就需要使用到flask框架默认设置的static目录方式即将这些图片、css、js文件存放到项目目录下的static目录然后使用路由指向。
创建static目录存入图片 html编写
添加如下代码 img src{{ url_for(static, filename风景.webp) }} alt风景图片
!DOCTYPE html
html langen
headmeta charsetUTF-8title网页标题/title
/head
bodyh1欢迎来到我的网页/h1img src{{ url_for(static, filename风景.webp) }} alt风景图片p这是一个简单的 HTML 示例。/p{{data}}
/body
/htmlurl_for函数用于生成静态文件的 URL将定义的静态文件显示到页面上 flask入门
网站多域名
from flask import Flask
from flask import render_templatewebapp Flask(__name__)webapp.route(/)
def index():return render_template(index.html)webapp.route(/login)
def login():return render_template(login.html)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue)再main文件里定义多个url路由 访问网址 127.0.0.1/login后调用login.html 用户登陆的login.html
!DOCTYPE html
html
headtitleUser Login/title
/head
bodyh2User Login/h2form action/login methodpostdivlabel forusernameUsername:/labelinput typetext idusername nameusername/divdivlabel forpasswordPassword:/labelinput typepassword idpassword namepassword/divdivinput typesubmit valueLogin/div/form
/body
/html
访问127.0.0.1/login 网站之间超链接跳转
访问默认网址网页显示一个可点击的链接可跳转到其他界面可以使用a超链接实现使用url_for函数
from flask import Flask
from flask import render_templatewebapp Flask(__name__)webapp.route(/)
def index():return render_template(index.html)webapp.route(/info)
def info():return render_template(info.html)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue)
index.html编写加入a超链接
ullia href/info去看相关介绍说明/a/li
/ul
!DOCTYPE html
html langen
headmeta charsetUTF-8title网页标题/title
/head
bodyh1欢迎来到我的网页/h1img src{{ url_for(static, filename风景.webp) }} alt风景图片p这是一个简单的 HTML 示例。/p{{data}}
ullia href/info去看相关介绍说明/a/li
/ul
/body
/html
点击“去看相关介绍说明”则跳转到/info这个界面
info.html
!DOCTYPE html
html
headtitleFlask常用方法介绍/title
/head
bodypstrongroute(rule, methods[GET])/strong 用于定义 URL 规则和请求方法的映射关系指定了响应请求的处理函数。/ppstrongrender_template(template_name_or_list, **context)/strong 用于渲染模板文件并传递上下文变量到模板中进行渲染。/ppstrongrequest.args/strong 获取请求 URL 中的查询参数。/ppstrongrequest.form/strong 获取 POST 请求发送的表单数据。/ppstrongrequest.files/strong 获取上传的文件数据。/ppstrongsession/strong 用于存储用户会话信息可以在不同请求之间共享数据。/ppstrongredirect(location, code302)/strong 重定向到指定的 URL。/ppstrongurl_for(endpoint, **values)/strong 生成指定端点对应的 URL。/ppstrongjsonify(*args, **kwargs)/strong 将传入的数据序列化为 JSON 格式并创建包含 JSON 数据的 Response 对象。/ppstrongmake_response(*args)/strong 创建一个自定义的响应对象可以设置 HTTP 状态码、头部信息等。/ppstrongabort(status_code)/strong 中止请求并返回指定的 HTTP 状态码。/p
/body
/html
看下访问结果
访问/ 显示: 点击链接跳转
可发现url变成了 /info 入门案例
将centos的rpm包下载链接集成到自己的服务器
1.编写代码
from flask import Flask
from flask import render_templatewebapp Flask(__name__)webapp.route(/)
def index():return render_template(index.html)webapp.route(/download)
def download():return render_template(rpm_list_download.html)if __name__ __main__:webapp.run(port8080, host127.0.0.1, debugTrue)2.导出html页面并放到template目录 3.编写Index.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title网页标题/titlestylebody {font-family: Arial, sans-serif;text-align: center;background-color: #f4f4f4;margin: 0;padding: 0;}#content-container {max-width: 600px;margin: 20px auto;padding: 20px;background-color: #fff;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);}h1 {color: #333;}img {max-width: 100%;height: auto;margin-bottom: 20px;}ul {list-style: none;padding: 0;}li {margin-top: 10px;}a {text-decoration: none;color: #007bff;}/style
/head
bodydiv idcontent-containerh1欢迎来到我的Centos Rpm下载页面/h1img srcstatic/centos.webp altCentosp{{data}}/pullia href/download 点击这里下载/a/li/ul/div
/body
/html4.访问127.0.0.1:8080 点击下载即可 文章转载自: http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.bppml.cn.gov.cn.bppml.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.dcccl.cn.gov.cn.dcccl.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn