美文网首页
Flask学习计划

Flask学习计划

作者: 冰西瓜大郎 | 来源:发表于2017-07-10 22:09 被阅读0次

从7.10开始学习Flask框架,到8月份,必须熟练的掌握Flask,能够快速的建站。下面只记录碰到的问题。
准备:Python2.7、Flask、虚拟环境virtualenv。

关于模版的jinja2,这个已经集成到Flask中。使用方法:在项目目录下建templates文件夹,里面可以放**.html文件。

举个例子:

<html>
  <head>
    {% if title %} #控制语句
    <title>{{title}} - microblog</title> #{{...}}填充内容
    {% else %}
    <title>Welcome to microblog</title>
    {% endif %}
  </head>
  <body>
      <h1>Hello, {{user.nickname}}!</h1>
  </body>
</html>

项目中如何和html文件建立连接:使用render_template函数。

from flask import render_template
def index():
    user = { 'nickname': 'Miguel' } # fake user
    posts = [ # fake array of posts
        { 
            'author': { 'nickname': 'John' }, 
            'body': 'Beautiful day in Portland!' 
        },
        { 
            'author': { 'nickname': 'Susan' }, 
            'body': 'The Avengers movie was so cool!' 
        }
    ]
    return render_template("index.html",
        title = 'Home',
        user = user,
        posts = posts)

相关文章

网友评论

      本文标题:Flask学习计划

      本文链接:https://www.haomeiwen.com/subject/tvhuhxtx.html