05 2023 档案
摘要:父模板 base.html 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <meta
阅读全文
摘要:宏 forms.html 1 {% macro input(name, value="",type="text") %} 2 <input type="{{ type }}" value="{{ value | escape }}" name="{{ name }}"> 3 {% endmacro
阅读全文
摘要:调用 1 @app.route('/for') 2 def for_statement(): 3 books = [{ 4 'title': '三国演义', 5 'author': '罗贯中', 6 'price': 100 7 }, 8 { 9 'title': '水浒传', 10 'author
阅读全文
摘要:调用 1 @app.route('/if') 2 def if_statement(): 3 age = 18 4 return render_template('if.html', age=age) if.html 1 <!DOCTYPE html> 2 <html lang="en"> 3 <h
阅读全文
摘要:在模板中,通过过滤器实现对变量的处理。 过滤器本质上是 Python 的函数,它会把被过滤器的值当作第1个参数传送给函数。 自定义过滤器 定义 1 def datetime_format(value, format="%Y-%m-%d %H:%M"): 2 return value.strftime
阅读全文
摘要:渲染模板 index.html 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首页</title> 6 </head> 7 <body> 8 <h1>这是首页</h1> 9 </body
阅读全文
摘要:通过 url_for 函数构造 URL。 url_for 接收视图函数名作为第1个参数,以及其他 URL 定义时的参数,其他参数添加到 URL 的后面作为查询字符串参数。 1 @app.route('/blog/<int:blog_id>') 2 def blog_detail(blog_id):
阅读全文
摘要:页面重定向:浏览器从一个页面自动跳转到另一个页面。 例如,用户访问一个需要权限的页面,但是该用户当前没有登录,因此重定向到登录页面。 永久性重定向 HTTP 的状态码是301。 暂时性重定向 HTTP 的状态码是302。 1 @app.route('/profile') 2 def profile(
阅读全文
摘要:- 请求某个 URL 时,要获取数据,用 GET 方法。 - 要删除服务器数据,用 DELETE 方法。 - 要往服务器添加数据,就用 POST 方法。 ```python @app.route('/blog/add/post/get', methods=['POST', 'GET']) def b
阅读全文
摘要:## 定义无参数的 URL  ## 定义有参数的 URL ![](https://img2023.cnblogs.
阅读全文
摘要:## URL - @app.route 中的第一个字符串参数叫作 URL。 - “/” 代表网站的根路径,只要在浏览器中输入网站的域名即可访问到 “/”。 ## 视图函数 - 被@app.route 装饰的函数叫作视图函数。
阅读全文
摘要:除了 Debug、Host、Port 这3个配置项比较特殊外,其他的配置参数都需要配置到 Flask 对象的 app.config 属性中,在配置参数较多的情况下,还会放到配置文件中。 使用 app.config 配置 app.config 是 Config 的对象,Config 是一个继承自字典的
阅读全文