zoukankan      html  css  js  c++  java
  • flask中的Flask、request、render_temple、redirect和url_for

    学flask也有一个多星期了,对这个web框架也有了一点的了解,梳理一些基础的知识点,还是小白一只,代码写得比较low,若文章有错误的地方欢迎大佬随时指正,代码中被注释掉的代码是关于预防csrf,无视即可

    主程序脚本:

     1 from flask import Flask, render_template, request, redirect, url_for
     2 
     3 # from flask_wtf import CSRFProtect
     4 
     5 app = Flask(__name__)      # 声明一个Flask的类,__name__参数的作用是为了确定程序的根目录,以便获得静态文件的模板文件
     6 # app.config["SECRET_KEY"] = "12345678"
     7 #
     8 # CSRFProtect(app)
     9 
    10 
    11 @app.route('/')    # @app.router()是一个装饰器,它的作用就是把试图函数(可以简单的理解成就是它下面的函数)与某一个url(后面括号中的部分)绑定,当访问这个url时,就会运行这个视图函数
    12 def Helloworld():
    13     return redirect(url_for('home'))    url_for对视图函数进行反转,第一个参数为视图函数名,如果视图函数有参数,可加在后面,返回url,redirect是重定向到括号里面的url,这里为url_for的返回值
    14 
    15 
    16 @app.route("/regist/", methods=["get", "post"])  # methods指定请求这个url的方法,默认是get,这里指定使用get或post两种方式
    17 def regist():
    18     if request.method == "GET":
    19         return render_template("csrf_regist.html") # render_template对页面进行渲染,如果页面中存在待接受的参数,可将参数放在后面
    20 
    21     else:
    22         print(request.form)  # 如果请求方式为post,则在后台输出用户输入的信息。request可以获取到前端用户输入的信息,request.args获取get请求,request.form获取post请求
    23 
    24         return redirect(url_for('login'))  # 对login视图函数进行反转,并进行重定向
    25 
    26 
    27 @app.route("/login/", methods=["get", "post"])
    28 def login():
    29     if request.method == "GET":
    30         return render_template("csrf_login.html")
    31 
    32     else:
    33         print(request.form)
    34 
    35         return redirect(url_for('home'))
    36 
    37 
    38 @app.route('/home/')
    39 def home():
    40     return render_template('csrf_home.html')
    41 
    42 
    43 if __name__ == "__main__":
    44     app.run()

    首页(csrf_home.html)代码

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>csrf首页</title>
     6 </head>
     7 <body>
     8 <h1>欢迎来到首页,请选择登陆或注册</h1>
     9 <a href="{{ url_for('login') }}">登陆</a>    # 当用户点击登陆时,跳转到登陆页面
    10 <a href="{{ url_for('regist') }}">注册</a>    # 当用户点击注册时,跳转到注册页面
    11 </body>
    12 </html>

    注册(csrf_regist.html)代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>csrf的注册页面</title>
    </head>
    <body>
        <form action="", method="post">
            <table>
                <tbody>
    {#                <td>#}
    {#                    <input type="text" name="csrf_token", value="{{ csrf_token() }}">#}
    {#                </td>#}
                    <td>
                        <tr>用户名:</tr>
                        <tr><input type="text" name="username" value=""></tr><br>
                    </td>
                    <td>
                        <tr>密码:</tr>
                        <tr><input type="password" name="password" value=""></tr><br>
                        <input type="submit" value="注册">
                    </td>
                </tbody>
            </table>
        </form>
    </body>
    </html>

    登陆(csrf_login.html)代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>csrf登陆页面</title>
    </head>
    <body>
        <form action="", method="post">
            <table>
                <tbody>
    {#                <td>#}
    {#                    <input type="text" name="csrf_token", value="{{ csrf_token() }}">#}
    {#                </td>#}
                    <td>
                        <tr>用户名:</tr>
                        <tr><input type="text" name="username" value=""></tr><br>
                    </td>
                    <td>
                        <tr>密码:</tr>
                        <tr><input type="password" name="password" value=""></tr><br>
                        <input type="submit" value="登陆">
                    </td>
                </tbody>
            </table>
        </form>
    </body>
    </html>
  • 相关阅读:
    前端开发 —— 快速入门:JavaScript & CSS 脚手架
    MySQL全方位练习(学生表 教师表 课程表 分数表)
    php判断是爬虫在访问还是用户浏览器在访问
    Eclipse 安装Activiti插件
    随机生成一串字符串(java)
    js文字转语音(speechSynthesis)
    语音识别(Web Speech API)
    js判断是否为手机端访问
    tomcat部署安全证书文件(阿里云SSL证书)
    springboot文字转语音(jacob)
  • 原文地址:https://www.cnblogs.com/liangshian/p/11199475.html
Copyright © 2011-2022 走看看