• [学习记录]flask中的一些http方法


    1.对于post方法与get方法采用不同的响应

    以官方文档demo为例

     1 @app.route('/login', methods=['GET', 'POST'])
     2 def login():
     3     if request.method == 'POST':
     4         session['username'] = request.form['username']
     5         return redirect(url_for('index'))
     6     return '''
     7         <form action="" method="post">
     8             <p><input type=text name=username>
     9             <p><input type=submit value=Login>
    10         </form>
    11     '''

    如果是get方法,则返回登陆表单

    如果是post方法,则记录会话内容并重定向到index

    2.重定向与错误

     1 from flask import abort, redirect, url_for
     2 
     3 @app.route('/')
     4 def index():
     5     return redirect(url_for('login'))
     6 
     7 @app.route('/login')
     8 def login():
     9     abort(401)
    10     this_is_never_executed()

    通过abort可以直接返回客户端一个指定的响应码,redirect则返回302,并附上重定向后的url地址

    3.请求对象

    用户的request请求被处理成一个对象,从flask中导入request后可以直接调用request查看请求中的属性,内容非常多

     1 def printrequest():
     2     print(request.accept_charsets)
     3     print(request.accept_encodings)
     4     print(request.accept_languages)
     5     print(request.accept_mimetypes)
     6     print(request.access_route)
     7     print(request.application)
     8     print(request.args)
     9     print(request.authorization)
    10     print(request.base_url)
    11     print(request.blueprint)
    12     print(request.cache_control)
    13     print(request.charset)
    14     print(request.close)
    15     print(request.content_encoding)
    16     print(request.content_length)
    17     print(request.content_md5)
    18     print(request.content_type)
    19     print(request.cookies)
    20     print(request.data)
    21     print(request.date)
    22     print(request.dict_storage_class)
    23     print(request.disable_data_descriptor)
    24     print(request.encoding_errors)
    25     print(request.endpoint)
    26     print(request.environ)
    27     print(request.files)
    28     print(request.form)
    29     print(request.form_data_parser_class)
    30     print(request.from_values)
    31     print(request.full_path)
    32     print(request.get_data)
    33     print(request.get_json)
    34     print(request.headers)
    35     print(request.host)
    36     print(request.host_url)
    37     print(request.if_match)
    38     print(request.if_modified_since)
    39     print(request.if_none_match)
    40     print(request.if_range)
    41     print(request.if_unmodified_since)
    42     print(request.input_stream)
    43     print(request.is_json)
    44     print(request.is_multiprocess)
    45     print(request.is_multithread)
    46     print(request.is_run_once)
    47     print(request.is_secure)
    48     print(request.is_xhr)
    49     print(request.json)
    50     print(request.list_storage_class)
    51     print(request.make_form_data_parser)
    52     print(request.max_content_length)
    53     print(request.max_form_memory_size)
    54     print(request.max_forwards)
    55     print(request.method)
    56     print(request.mimetype)
    57     print(request.mimetype_params)
    58     print(request.on_json_loading_failed)
    59     print(request.parameter_storage_class)
    60     print(request.path)
    61     print(request.pragma)
    62     print(request.query_string)
    63     print(request.range)
    64     print(request.referrer)
    65     print(request.remote_addr)
    66     print(request.remote_user)
    67     print(request.routing_exception)
    68     print(request.scheme)
    69     print(request.script_root)
    70     print(request.shallow)
    71     print(request.stream)
    72     print(request.trusted_hosts)
    73     print(request.url)
    74     print(request.url_charset)
    75     print(request.url_root)
    76     print(request.url_rule)
    77     print(request.user_agent)
    78     print(request.values)
    79     print(request.view_args)
    80     print(request.want_form_data_parsed)
  • 相关阅读:
    安装APK失败,错误代码:INSTALL_FAILED_INVALID_APK 解决方案
    android保持服务不休眠(持续运行)以及唤醒屏幕的方法
    判断Android 当前版本是否为debug版本
    Android 使用WebView加载含有Canvas的页面截屏处理
    喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库
    系统架构设计理论与原则、负载均衡及高可用系统设计速记
    Sharing A Powerful Tool For Application Auto Monitor
    Sharing A Powerful Tool For Calculate Code Lines
    关于GC和析构函数的一个趣题
    垃圾回收机制GC知识再总结兼谈如何用好GC
  • 原文地址:https://www.cnblogs.com/trickofjoker/p/11058512.html
Copyright © 2020-2023  润新知