zoukankan      html  css  js  c++  java
  • 02模板渲染和参数(补充:URL传参到视图)

    先抛出代码:

    @app.route('/')
    def index():
        return render_template('index.html',username ="郑勇")

    问题:形如“username”的参数过多会影响维护,解决方案?

    用字典的形式写就好。

    @app.route('/')
    def index():
        content = {
            'username': "郑勇",
            'gender': "",
            'age': "24"
        }
        return render_template('index.html',**content) #注意这里是**号,这里把这个content字典一个个填充进去

    当然还有第二种方式

    @app.route('/')
    def index():
        class Person(object):
            name = "啧啧"
            age = "18"
    
        p = Person()
        content = {
            'username': "哈哈",
            'gender': "",
            'age': "20",
            'person' : p   #这里千万别忘了
        }
        return render_template('index.html',**content)

    此时index文件这样写:

    <p>user:{{ person.name }}</p>  #person对应
    <P>age :{{ person.age }}</P>

    第三种方法,字典中的字典:

    @app.route('/')
    def index():
        class Person(object):
            name = "啧啧"
            age = "18"
    
        p = Person()
        content = {
            'username': "哈哈",
            'gender': "",
            'age': "20",
            'person' : p,
            'websites' :{
                "baidu" : "www.baidu.com",
                "taobao" : "www.taobao.com",
            }
        }
        return render_template('index.html',**content)

    index文件同理:

    <p>user:{{ person.name }}</p>
    <P>age :{{ person.age }}</P>
    <hr>
    <p>网址:{{ websites.baidu }}</p>

     补充:URL传参到视图

  • 相关阅读:
    解决Cannot change version of project facet Dynamic web module to 3.0
    mysql 存储过程
    查看建表语句
    mysql query cache 查询缓存
    数据库连接池
    JDBC Statement PrepareStatement
    mysql 改变表结构 alter
    maven 获取pom.xml的依赖---即仓库搜索服务
    windows常用快捷键
    oracle 的数据完整性
  • 原文地址:https://www.cnblogs.com/two-peanuts/p/10538152.html
Copyright © 2011-2022 走看看