Flask初学

##1.render_template模板的调用

**from flask import Flask,render_template
@app.route('/')**
def hello_world():
    context = {
        'username':'xiaowen',
        'height':'181cm',
        'age':18,
        'child':{
            'name':'xiaoxiaowen',
            'height':'100cm'
        }
    }
    return render_template('index.html',**context)

模板的位置放在templates文件夹下面,一般是html文件,{{}}表示这是一个变量,可以根据用户在模块端给予的参数的不同,进行调整。**解包参数。

@app.route('/profile/',methods=['GET','POST'])
def profile():
    name = request.args.get('username')
    if not name:
        return redirect(url_for('logins'))
    else:
        return '大金链子小手表,一天三顿小烧烤'

传递methods参数改变请求方式获取两种请求方式,并通过request获取前端提交的参数,若无username提交时则通过redirect重定向(关于定向:状态码 3开头表示重定向 301 永久重定向 302 临时重定向 ),需要使用url_for获取login视图函数的路由。

##2.include (包含):一个页面包含另外一个页面

@app.route('/')
def hello_world():
    return render_template('index.html',username='kangbazi')

以下是index块

<body>
    {% include 'common/header.html' %}
    <div class="container">
        中间的内容
{#  不从后台过来 页面自己设置变量  set全局设置#}
        {% set country="china" %}
        <p>{{ country }}</p>
{#        with的话 只能在这个区域里边才行 #}
        {% with  %}
            {% set age=18 %}
            <p>{{ age }}</p>
        {% endwith %}


    <p>{{ age }}</p>

    </div>
    {% include 'common/footer.html' %}
</body>

头文件 {% include ‘common/header.html’ %} ----包含header.html里面的页面 ,注意set和with作用域的区别,with下的set只在本区域可用
尾部页面{% include ‘common/footer.html’ %}。

##3.inherit(继承):

{% extends 'base.html' %}##引入继承页面
{% block headers %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css.css') }}">
{% endblock %}
{% block title %}
    首页
{% endblock %}##到此处结束
{% block content %}
    <div class="test">
        <p>{{username}}</p>
    </div>
    {{ super() }}
    <h1>我是首页的内容</h1>
    <div style="background:pink;width: 100px;height:100px;">我是子模板的内容</div>
    <img src="{{ url_for('static',filename='timg.jpg') }}" alt="美女">
{% endblock %}

extends开始,endblock结束

{% block content %}
           <div style="background: yellow;color: rebeccapurple;width: 200px;height: 200px"> 我爱姑娘</div>
        {% endblock %}

{% block content %},允许子页面改动的地方,子页面的。静态文件加载第一个参数必须是static,{{ super() }}既显示base.html页面内的“我爱姑娘”的block块又显示自己的内容。

##4.response
主要用于参与response结果,制定浏览器返回结果,可用make_response进行创建respons

@app.route('/')
def index():
    resp = Response(response='千锋首页',status=500,content_type='text/html;charset=utf-8')
    return resp

##5.宏,宏是一个特殊函数但是没有返回值。
Jinja2支持宏,还可以导入宏,需要在多处重复使用的模板代码片段可以写入单独的文件,再包含在所有模板中,以避免重复。
定义带参数的宏,此处是封装成单独的html文件:

{% macro inputs(name="",value="",placeholder="",type="text") %}
    <input type="{{ type }}" name="{{ name }}" value="{{ value }}">
{% endmacro %}

在其他文件中需要先导入再调用:

{% import 'macro/macro.html' as macros with context %}
.
.
{% from 'macros/macros.html' import itemGroup %}

导入模块下的itemgrop函数
还有很多细节,没得时间整理。这只是很小的一部分。哈哈哈,第一篇就这么先将就一下。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值