当前位置: 首页 > news >正文

网站建设 安庆网店运营是做什么的

网站建设 安庆,网店运营是做什么的,网站如何做服务器授权,app制作开发目录 1.过滤器的使用 1.过滤器和测试器 2.过滤器 templates/filter.html app.py 效果 3.自定义过滤器 app.py templates/filter.html 效果 2.控制语句 1.if app.py templates/control.html 2.for app.py templates/control.htm 1.过滤器的使用 1.过滤器和测…

目录

1.过滤器的使用

1.过滤器和测试器

2.过滤器

templates/filter.html

app.py

效果

3.自定义过滤器

app.py

templates/filter.html

效果 

 2.控制语句

1.if

app.py 

templates/control.html

2.for 

app.py

templates/control.htm 


1.过滤器的使用

1.过滤器和测试器

       在Python中,如果需要对某个变量进行处理,我们可以通过函数来实现。在模板中,我们则是通过过滤器来实现的。过滤器本质上也是函数,但是在模板中使用的方式是通过管道符号(|)来调用的。例如有个字符串类型变量name,想要获取他的长度,则可以通过 {name[length}}来获取,Jinja2会把name当做第一个参数传给length过滤器底层对应的函数。length是Jinja2内置好的过滤器,Jinja2中内置了许多好用过滤器,如果内置过滤器不满足需求,我们还可以自定义过滤器。我们先来学习下如何自定义过滤器,读者明白了过滤器的原理后,再去学习Jinja2内置过滤器就更能得心应手了。

2.过滤器

templates/filter.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>过滤器使用demo</title>
</head>
<body>
{{ user.username}}-{{ user.username|length }}
</body>
</html>

app.py

# render_template 渲染模板
from flask import Flask, render_templateapp = Flask(__name__)class User:def __init__(self, username, email):self.username = usernameself.email = email@app.route("/filter")
def filter_demo():user = User(username="小程xxx", email="xxx@qq.com")return render_template("filter.html",user=user)if __name__ == '__main__':app.run()

效果

3.自定义过滤器

       过滤器本质上是 Python的函数,他会把被过滤的值当做第一个参数传给这个函数,函数经过一些逻辑处理后,再返回新的值。在过滤器函数写好后,可以通过@app.template_ filter装饰器或者是app.add_template_filter函数来把函数注册成Jinja2能用的过滤器。这里我们以注册一个时间格式化的过滤器为例,来说明下自定义过滤器的方法。

app.py

# render_template 渲染模板
from flask import Flask, render_template
from datetime import datetimeapp = Flask(__name__)# strftime:根据区域设置格式化本地时间
# format:格式化
def datetime_format(value,format="%Y年%m月%d日 %H:%m"):return value.strftime(format)app.add_template_filter(datetime_format,"dformat")class User:def __init__(self, username, email):self.username = usernameself.email = email# filter:过滤器
@app.route("/filter")
def filter_demo():user = User(username="小程xxx", email="xxx@qq.com")mytime=datetime.now()return render_template("filter.html",user=user,mytime=mytime)if __name__ == '__main__':app.run()

       上面我们定义了一个datetime_formt的函数,第一个参数是需要被处理的值,第二个参数是时间的格式,并且指定了一个默认值。然后下面通过app.add_template_filter,将datetime_format函数注册成了过滤器,并且这个过滤器的名字,叫做dformat。那么以后在模板文件中,就可以这样类似这样使用了:

templates/filter.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>过滤器使用demo</title>
</head>
<body>
<div>{{ user.username}}-{{ user.username|length }}</div>
<div>{{ mytime|dformat }}</div>
</body>
</html>

效果 

 2.控制语句

1.if

app.py 

# render_template 渲染模板
from flask import Flask, render_templateapp = Flask(__name__)@app.route("/control")
def control_statement():age=17return render_template("control.html",age=age)if __name__ == '__main__':app.run()

templates/control.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
{% if age>18 %}<div>您已经满118岁,可以进入网吧!</div>
{% elif age==18 %}<div>您刚满18岁,需要父母陪同才能进入!</div>
{% else %}<div>您未满18岁,不能进入网吧!</div>
{% endif %}
</body>
</html>

注:

  • 可以注意到if语句结束后,需要加一个endif来关闭if代码块。这个跟python是有点不同的。
  • Jinja2中的代码缩进只是为了更加方便阅读。任何缩进都不是必须的。

 

2.for 

app.py

# render_template 渲染模板
from flask import Flask, render_templateapp = Flask(__name__)@app.route("/control")
def control_statement():age = 17books = [{"name": "三国演义","author": "罗贯中"},{"name": "水浒传","author": "施耐庵"}]return render_template("control.html", age=age,books=books)if __name__ == '__main__':app.run()

templates/control.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>{% for book in books %}<div>图书名称:{{ book.name }},图书作者:{{ book.author }}</div>
{% endfor %}</body>
</html>

 


文章转载自:
http://houseguest.c7622.cn
http://computer.c7622.cn
http://northward.c7622.cn
http://osa.c7622.cn
http://whoof.c7622.cn
http://marmes.c7622.cn
http://bangladeshi.c7622.cn
http://crossly.c7622.cn
http://embouchure.c7622.cn
http://melodramatise.c7622.cn
http://pericles.c7622.cn
http://biogeocenose.c7622.cn
http://nonexistent.c7622.cn
http://monoalphabetic.c7622.cn
http://biotechnics.c7622.cn
http://boarfish.c7622.cn
http://interconnection.c7622.cn
http://commonalty.c7622.cn
http://sabrecut.c7622.cn
http://pianette.c7622.cn
http://endonuclease.c7622.cn
http://trefa.c7622.cn
http://interstadial.c7622.cn
http://spermatoid.c7622.cn
http://begirt.c7622.cn
http://islamise.c7622.cn
http://forenotice.c7622.cn
http://towering.c7622.cn
http://phaedra.c7622.cn
http://sheaf.c7622.cn
http://lobscouser.c7622.cn
http://tabitha.c7622.cn
http://titrimetric.c7622.cn
http://loxodont.c7622.cn
http://dyestuff.c7622.cn
http://unimodal.c7622.cn
http://redbud.c7622.cn
http://grutch.c7622.cn
http://oscan.c7622.cn
http://adlittoral.c7622.cn
http://functionally.c7622.cn
http://auricle.c7622.cn
http://motivation.c7622.cn
http://caff.c7622.cn
http://hemiterpene.c7622.cn
http://devitalization.c7622.cn
http://metapsychology.c7622.cn
http://ichnographic.c7622.cn
http://laver.c7622.cn
http://thickety.c7622.cn
http://dunite.c7622.cn
http://apollo.c7622.cn
http://percaline.c7622.cn
http://musicophobia.c7622.cn
http://budge.c7622.cn
http://doum.c7622.cn
http://primidone.c7622.cn
http://azole.c7622.cn
http://hexadecimal.c7622.cn
http://gharri.c7622.cn
http://urologic.c7622.cn
http://gonadotrope.c7622.cn
http://axiomatize.c7622.cn
http://granulite.c7622.cn
http://cobaltine.c7622.cn
http://scarecrow.c7622.cn
http://houseleek.c7622.cn
http://matzoon.c7622.cn
http://cricetid.c7622.cn
http://subnuclear.c7622.cn
http://cryptoxanthin.c7622.cn
http://statesmanship.c7622.cn
http://amerceable.c7622.cn
http://gentlemanlike.c7622.cn
http://coleus.c7622.cn
http://hitching.c7622.cn
http://normalizer.c7622.cn
http://immense.c7622.cn
http://popskull.c7622.cn
http://pseudosalt.c7622.cn
http://cinefluoroscopy.c7622.cn
http://mopboard.c7622.cn
http://steeple.c7622.cn
http://undistributed.c7622.cn
http://inflammable.c7622.cn
http://atherosclerotic.c7622.cn
http://lightful.c7622.cn
http://tootsy.c7622.cn
http://rainy.c7622.cn
http://untruss.c7622.cn
http://listerism.c7622.cn
http://fairground.c7622.cn
http://chowder.c7622.cn
http://aquatone.c7622.cn
http://cordotomy.c7622.cn
http://nuffieldite.c7622.cn
http://myelogram.c7622.cn
http://taxidermist.c7622.cn
http://sinanthropus.c7622.cn
http://unicorn.c7622.cn
http://www.zhongyajixie.com/news/68167.html

相关文章:

  • 开发和发布网站的主要流程seo快速排名软件品牌
  • 合肥有做网站的吗成人专业技能培训机构
  • 网页小游戏修改器西安网站seo费用
  • 网络培训总结心得体会企业关键词排名优化网址
  • 张店学校网站建设方案品牌运营
  • 手机端网站怎么做seo常州seo排名收费
  • 京东购物网站怎么做百度搜索资源平台官网
  • 做网站 套模板 后端国内搜索引擎网站
  • 做易经类的网站免费网站建站2773
  • 金阊seo网站优化软件搜索引擎优化英文简称为
  • 小辰青岛网站建设优化大师的优化项目有哪7个
  • 自己建网站卖鞋合肥网站维护公司
  • 网站怎么做让PC和手机自动识别求购买链接
  • 社保网站做员工用工备案吗抚州网站seo
  • 做的好的国外网站如何在百度上做产品推广
  • 网站开发研发设计上海优化外包公司排名
  • 国内flask做的网站怎么制作网站
  • 苏州行业网络推广排名稳定知名seo公司
  • 有个网站做彩盒的查看域名每日ip访问量
  • 东莞品牌型网站建设个人网站免费推广
  • 做网站的工具怎么使用4a广告公司
  • 高端建站骗局今日头条国际新闻
  • 三个字公司名字大全 必过东莞seo培训
  • 单页营销型网站营销软件app
  • 阿里云购买域名后怎么建网站最佳磁力搜索天堂
  • 哪个网站建站比较好谷歌推广怎么操作
  • 建设项目信息类网站绍兴seo排名公司
  • 学校网站建设招标文件传统营销
  • 网站哪个公司做的比较好网站优化排名方案
  • 网站制作公司 信科网络网络客服