python Web开发 flask轻量级Web框架

python Web开发 flask轻量级Web框架O flask 介绍 Flask 是一个使用 Python 编写的轻量级 Web 应用框架 其 WSGI 工具箱采用 Werkzeug 模板引擎则使用 Jinja2 Flask 使用 BSD 授权 Flask 也被称为 microframewo 因为它使用简单的核心 用 extension 增加其他功能 Flask 没有默认使用的数据库 窗体验证工具 Web 应用采用访问和响应的方式和用户进

一、flask应用的基本构成

#coding=utf-8 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return "hello world" if __name__ == '__main__': app.run() 

app = Flask(name)
这是建立一个flask应用,并且设置flask应用的目录为该模块的目录。

运行程序,在浏览器中访问服务器地址即可得到返回的网页

@app.route('/users/<id>') def hello_users(id): return "users: " + id 

这里写图片描述

 @app.route('/user', methods=['POST']) def hello_user(): return "hello user" 
from flask import Flask,request 
@app.route('/query_user') def query_user(): id = request.args.get('id') return "query_user: id " + id 
@app.route('/query_url') def query_url(): return 'query_url' + url_for('query_user') 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>{
  
  
  
  
  
  
  
  { content }}hello world this is first web</h1>
</body>
</html>
@app.route('/')
def hello_world():
    return render_template("demo.html", content="hello flask ")

这里写图片描述

2.传入类

 class User: def __init__(self, user_id, user_name): self.user_id = user_id self.user_name = user_name 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>User id is {
  
  
  
  
  
  
  
  { user.user_id }}</p> <p>User name is { 
  { user.user_name }}</p>
</body>
</html>
@app.route('/user')
def hello_user():
    user = User(1, "hello user")
    return render_template("demo.html", user=user)

这里写图片描述

  1. 模板的 if 语法
    query_user.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% if user %}
{
  
  
  
  
  
  
  
  { user.user_name }}
{% else %}
<p> hello world</p>
{% endif %}
</body>
</html>

如果user不为空则输出 user.name 否则 输出hello world

@app.route('/query_user/<id>')
def query_user(id):
    user = None
    if id == '1':
        user = User(2, "zzm")
    return render_template("query_user.html", user = user)

这里写图片描述
这里写图片描述

4.模板循环

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for user in users %}
 {
  
  
  
  
  
  
  
  { user.user_id }}----{ 
  { user.user_name }}<br/>
{% endfor %}
</body>
</html>
@app.route('/list')
def user_list():
    users = []
    for i in range(1, 11):
        user = User(1, "zzm" + str(i))
        users.append(user)
    return render_template("user_list.html", users=users)

这里写图片描述

<div> 这是头部 </div> {% block content %} {% endblock %} <div> 这是尾部 </div> 
{% extends "base.html"%}
{% block content %}
    <h2>这是第一页</h2>
{% endblock %}
@app.route('/one')
def one_base():
    return render_template("one_base.html")
 <form action="/login" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="提交"> </form> <h2>{ 
  { get_flashed_messages()[0] }}</h2> 
@app.route('/flashDemo')
def flash_demo():
    flash("hello world flash")
    return render_template("flash.html")

这里写图片描述

写个404.html

<h1>this is error</h1> 
@app.errorhandler(404)
def not_fond(e):
    return render_template("404.html")

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233514.html原文链接:https://javaforall.net

(0)
上一篇 2025年7月12日 下午6:01
下一篇 2025年7月12日 下午6:22


相关推荐

  • pycharm中的manage repositories为空

    pycharm中的manage repositories为空今天想用 pycharm 安装第三方库时 发现 availablepac 为空 上网查资料发现大多数都是更新镜像源 但是我更新镜像源后一直在转圈 并且点确定后关闭再打开里面还是空 后面去外网看了看 结果发现是使用 conda 软件包管理的原因 只需要在这里把它关掉之后发现全都回来了

    2026年3月27日
    2
  • 请简述什么是Vue组件化开发_nodejs和vue的关系

    请简述什么是Vue组件化开发_nodejs和vue的关系前言真实项目开发过程中,我们都是使用组件化的去开发vue的项目,但是组件化的思想又是如何来的呢?下面就从开始讲解演变过程演变过程1.0一般情况下vue都是单页面开发,所以项目中只会有一个inde

    2022年8月7日
    9
  • PyCharm将py文件文件生成可exe可执行文件[通俗易懂]

    PyCharm将py文件文件生成可exe可执行文件[通俗易懂]1.安装pyinstraller工具2.在PyCharm最下方找到终端,在终端输入pyinstaller-F–onefilexxx.py

    2022年8月27日
    7
  • android 修改标题栏文字居中

    android 修改标题栏文字居中android 修改 ToolBar 的 title 文字居中及注意事项 1 先创建一个布局文件 title bar xml 里面就是标题栏的内容 根据自己需求来调整 lt xmlversion 1 0 encoding utf 8 gt lt RelativeLayo android http schemas android com apk res a

    2026年3月26日
    2
  • 【云原生】微服务之Feign的介绍与使用

    【云原生】微服务之Feign的介绍与使用本文介绍了微服务中 Feign 的使用 正在学习 Feign 或者准备学习的朋友看过来哟

    2026年3月16日
    3
  • pycharm下配置conda虚拟环境labelme

    pycharm下配置conda虚拟环境labelmepycharm 下配置 conda 虚拟环境文章目录 pycharm 下配置 conda 虚拟环境 1 下载 Anaconda32 安装 Anaconda3 使用 AdacondaPowe 创建虚拟环境 env4 pycharm 创建工程 使用虚拟环境 labelme5 打开 labelme 工具 1 下载 Anaconda3 在清华镜像下载 Anaconda3htt mirrors tuna tsinghua edu cn anaconda archive 2 安装 Anaconda3 使用 Ad

    2026年3月27日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号