Flask快速入门

分类: Python / 发布于2018-6-12
 84 人气 / 0 评论

1.安装flask
sudo easy_install Flask

2.快速入门



#!/usr/bin/env python
#coding=utf-8
 
from flask import Flask,url_for,request,render_template,redirect,abort,escape,session
from werkzeug import secure_filename
 
print __name__
app = Flask(__name__)
app.secret_key = 'hello'
 
@app.route('/')
def index():
    return "hello flask"
 
@app.route('/user/<username>')
def show_username(username):
    return username
 
@app.route('/post/<int:post_id>')
def show_post(post_id):
    return 'post_id:%d' % post_id
 
#if you visit /projects will redirect /projects/
@app.route('/projects/')
def projects():
    return 'the project page'
 
#if you visit /about/ will return 404 error
@app.route('/about')
def about():
    return 'the about page'
 
@app.route('/upload/',methods=['GET','POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['the_file']
        f.save('./'+secure_filename(f.filename))
        return secure_filename(f.filename)
    else:
        return render_template('upload_file.html')
 
@app.route('/redirect/')
def test_redirect():
    return redirect(url_for('test_error'))
 
@app.route('/test_error/')
def test_error():
    #中断请求,并返回错误码
    abort(404)
 
@app.errorhandler(404)
def page_not_found(error):
    print error
    return render_template('page_not_found.html'),404
 
@app.route('/index/')
def test_session():
    if 'username' in session:
        return 'logged in as %s' % escape(session['username'])
    return redirect(url_for('login'))
 
@app.route('/login/',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('test_session'))
    else:
        return '''
        <form action="/login/" method="post">
        <input type=text name=username>
        <input type=submit value=login>
        </form>
        '''
@app.route('/logout/')
def logout():
    session.pop('username',None)
    return redirect(url_for('test_session'))
 
with app.test_request_context():
    print url_for('index')
    print url_for('index',next='/')
    print url_for('show_username',username='alex')
 
@app.route('/setcookie')
def set_cookie():
    if 'num' in request.cookies:
        count = int(request.cookies['num']) + 1
    else:
        count = 0
 
    #每个view最后返回的都是response对象,render_template内部做了处理
    #也可以这样表示response = make_response(render_template('index.html', count=100))
    #不设置max_age和expires时,默认是会话cookie,浏览器关闭后cookie就失效
    #domain可以设置跨域cookie,如domain=".example.com",这样cookie可以 被"www.example.com,alex.example.com"共享
    response = app.make_response(str(count)) 
    response.set_cookie('num',value=count,max_age=None,expires=None,domain=None)
    return response
 
if __name__ == '__main__':
    app.run(host="localhost",port=8888,debug=True)


打赏
X
打赏方式:
  • 支付宝
  • 微信
  • QQ红包

打开支付宝扫一扫
相关资源:
  • 分分钟学会用python爬取心目中的女神
    分分钟学会用python爬取心目中的女神
    2016-09-07 0 884
  • python requests的安装与简单运用
    python requests的安装与简单运用
    2017-05-24 0 942
  • Python3爬虫实例——使用Scrapy重构代码爬取名著
    Python3爬虫实例——使用Scrapy重构代码爬取名著
    2016-11-06 0 1739
  • 最难搞的python“反面”代码
    最难搞的python“反面”代码
    2017-02-09 0 1144

评论