学习日记 | 5.28 [Python3] Python Web开发基础

注:这是一系列基于实验楼网络培训的python学习日记,内容零散,只是便于我自己回顾,有需要请了解www.shiyanlou.com。


1. 实验11: Flask入门

 flask微框架相当于一个设计精良的cpu,可以通过许多插件来扩展。

创建目录,写入app,通过export设置应用变量,再用flask run来启动,进入 localhost:5000 可以查看结果。

flask run是直接进入app运行,而flask shell只加载并进入一个shell终端,在这个终端中可以执行后续代码。

通过app.config进行配置管理。大型项目一般有一个单独的config.py文件。

flask使用装饰器 @app.route 来注册路由及处理函数,可以在路由中传入变量。

from flask import Flask

app = Flask(__name__)

app.config.update({
    "SECRET_KEY":"a random string"})

@app.route("/")
def index():
    return("Hello World!")

@app.route("/user/<username>")
def user_index(username):
    return "Hello {}".format(username)

@app.route("/post/<int:post_id>")
def show_post(post_id):
    return "Hello {}".format(post_id)

if __name__ == "__main__":
    app.run()

flask能够通过调用制定模板渲染html界面,默认模版引擎为jinja2,创建templates文件夹,使用render_template函数渲染模版:

from flask import render_template

@app.route('/user/<username>')
def user_index(username):
    return render_template('user_index.html', username=username)

用request获取对象相关数据:

访问主页为:

localhost:5000?page=2&per_page=10

获取参数:

page = request.args.get('page')
per_page = request.args.get('per_page')

http协议是无状态的,需要服务器记住但不必存入数据库的数据就被存放在session中。

cookies与session类似,是存放在客户端的加密信息:

from flask import Flask, render_template, make_response, request
app = Flask(__name__)
@app.route('/user/<username>')
def user_index(username):
    resp = make_response(render_template('user_index.html', username=username))
    resp.set_cookie('username', username)
    return resp

@app.route('/')
def index():
    username = request.cookies.get('username')
    return 'Hello {}'.format(username)

if __name__ == "__main__":
app.run()

 错误处理:

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

2. 实验12: html和css

html:

<!DOCTYPE html>
<html>
<head>
    <!-- 指定页面编码 -->
    <meta charset="utf-8">
    <!-- 指定页面 title,显示在浏览器上的信息 -->
    <title>HTML5 学习</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- 页面正式内容 -->
    <nav> 导航条 </nav>
    <div class="container"> 主体内容 </div>
    <footer>
        &copy; Copyright 2017 by <a href="http://www.shiyanlou.com/">shiyanlou</a>
    </footer>
</body>
</html>

学习html5的过程就是学习各种标签的属性和用途,html5比html好的一点在于可以使用语义标签,通过字面意思就能知道用途,不需要再引用标签的class和id。

并且表单功能进一步加强,示例如下:

<legend>表单示例</legend>
    <form action="" method="POST" id="form1">

        <input type="text" autofocus="autofocus" required  name="auto" placeholder="必填项测试">
        <input type="email" placeholder="请输入邮箱" name="mail">
        <input type="url" name="url" placeholder="输入正确的网址">
        <input type="password" name="password" placeholder="输入密码">
        <br>
        <br>
        <input type="submit" value="提交表单">
    </form>

层叠样式表css可以控制标签如何显示,可以通过<style></style>直接写在页面中,或者单独写在style.css文件中,语法由选择器和属性、值组成:

h1, h2, h3, ..., h6 {
    property1: value1; 
    property2: value2;
    ... 
    propertyN: valueN;
}

html子元素将继承父元素属性。

派生选择器

li strong{
    color: red;
}

id选择器:

#pid a{
    color:#00755f;
}

类选择器:

.container {
    color: red;
}

属性选择器:

[title] {
    color: red;
}

测试html:

<DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HTML5 study</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <nav>nav bar</nav>
    <div class="container">main content
        <h1>shiyanlou</h1>
        <p>
        <strong>I am not in the list so I wont change</strong>
        </p>
        <div class="box">
        </div>
        <ul>
            <li><strong>I am blue cuz strong is within li</strong></li>
        </ul>
        <legend> form sample </legend>
        <form action="" method="POST" id="form1">
            <input type="text" autofocus="autofocus" required name="auto" placehoder="test required item">
            <input type="email" placeholder="input the email" name="mail">
            <input type="url" name="url" placeholder="input the url">
            <input type="password" name="password" placeholder="input the piN">
            <br>
            <br>
            <input type="submit" value="submit the form">
        </form>
    </div>
    <footer>
        &copy; Copyright 2017 by <a href="http://www.shiyanlou.com/">shiyanlou</a>
    </footer>
</body>
</html>

测试css:

h1 {
    color:red;
    font-size: 40px;
}

li strong{
    color: blue;
}

* {
    margin: 0;
    padding: 0;
}

.box {
    width: 70px;
    height: 30px;
    margin: 60px;

    border-style: solid;
    border-width: 50px;

    padding: 50px;
}

猜你喜欢

转载自www.cnblogs.com/squidGuang/p/9098777.html