牛客网初级项目笔记(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Neo233/article/details/81428156

一. 装饰器

请参考:python装饰器

通俗不严谨解释:
把函数名称当作参数和返回值,在内部调用,控制调用前后的操作逻辑

二. Flask

请参考:Flask入门

流程

流程

一个最简易的网页

#-*- encoding=UTF-8 -*-

from flask import Flask 

app = Flask(__name__)    

@app.route('/')   
def index():
    return 'hello'

if __name__ == '__main__':
    app.run(debug=True)

简单的网页就运行起来了,下面就是针对这个网站进行一些细节操作,逐步完善

优化

多映射

  • 意义
    避免因后期升级导致旧的连接无法使用
  • 语法
    在上面添加装饰器,使得可以同时接受多个路径的映射
@app.route('/') #指定根路径的回调
@app.route('/index/')
def index():
    return 'hello'

 

参数变量

语法
通过url传递参数

@app.route('/profile/<uid>/')
def profile(uid):
    return 'profile : ' + uid

 

错误

指定传递参数的类型,如果传递的参数类型不符合,会直接回404

@app.route('/profileInt/<int:uid>/')
def profileInt(uid):
    return 'profileInt : ' + str(uid)

 

类型正确

类型错误

三. 模板(template)

请参考:Jinja2模板

课上的例子

  1. 导入render_template
  2. 在主程序所在目录下创建templates文件夹
  3. 把模板文件放到这里面

视频里的模板文件是profile.html

<html>
<body>
{# 这个是Jinja2模板语法,这个是注释 #}
uid={{uid}}<br>     {# 这是获取变量 #}

{% for i in range(0,5): %}
    {{loop.index}}, number:{{i}}<br>
{% endfor %}

{# 可以理解定义了一个带参数的宏 #}
{% macro render_color(color) %}
    <div>This is color {{color}}</div>  {{caller()}}
{% endmacro %}

{% for color in colors: %}
<br>
    {% call render_color(color) %}  {# 调用 #}
        render_color_demo<br>
    {% endcall %}
{% endfor %}
</body>
</html>
#-*- encoding=UTF-8 -*-

from flask import Flask, render_template #导入模块

app = Flask(__name__)   #创建对象

@app.route('/') #指定根路径的回调
@app.route('/index/')
def index():
    return 'hello'

@app.route('/profile/<uid>/')
def profile(uid):
    #return 'profile : ' + uid
    colors = ('blue', 'red', 'green')
    return render_template('profile.html', uid=uid, colors=colors)

@app.route('/profileInt/<int:uid>/')
def profileInt(uid):
    return 'profileInt : ' + str(uid)

if __name__ == '__main__':
    app.run(debug=True) # debug,开启debug模式

404模板–from flask import Flask, render_template, request

 
 
404的模板是not_found.html, 也要放在templates里面
添加request模块,获取当前请求的url
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Not Found HeiHei</title>
</head>
<body>
    {{request}}
</body>
</html>
from flask import Flask, render_template, request

@app.errorhandler(404)
def not_found(error):
    return render_template('not_found.html', request=request.url)

 


重定向—from flask import Flask, render_template, request, redirect

重定向就是将网页自动转向重定向,分为——301永久性重定向和302临时性重定向。

  • 301:新网址完全继承旧网址,旧网址的排名等完全清零
  • 302:对旧网址没有影响,但新网址不会有排名
from flask import Flask, render_template, request, redirect 

@app.route('/newpath')
def newpath():
    return 'newpath'

@app.route('/redirct/<int:number>')
def redirctPage(number):
    return redirect('/newpath',code=number)

猜你喜欢

转载自blog.csdn.net/Neo233/article/details/81428156
今日推荐