Flask快速入门

声明

原文地址http://codingnow.cn/server/409.html

#!/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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本篇我们运用<Web框架如何快速入门>的原理来制作一篇Python-Flask框架的快速入门指南来实战之前的原理,...
    真压力很大同志阅读 7,475评论 0 3
  • Flask开发环境配置Flask快速入门Flask实践Step by Step -- 'Hello World'F...
    CoderMiner阅读 5,342评论 0 3
  • Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务。本文...
    乐百川阅读 15,438评论 6 43
  • Flask is a microframework for Python based on Werkzeug, J...
    stiller阅读 1,801评论 0 1
  • 随着生产和科学技术的不断发展,目前金属焊接方法的种类很多,按照焊接过程的特点区分,主要可以归纳为三大类:熔焊、压焊...
    乐在释怀阅读 10,395评论 0 0

友情链接更多精彩内容