flask 第一章
Upload.py
1 #!/usr/bin/env python 2 #coding=utf-8 3 import os 4 from flask import Flask, request, redirect, url_for,render_template 5 from werkzeug.utils import secure_filename 6 7 UPLOAD_FOLDER = '/static/uploads' 8 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) 9 10 app=Flask(__name__) 11 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 12 13 def allowed_file(filename): 14 return '.' in filename and \ 15 filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS 16 17 @app.route('/', methods=['GET', 'POST']) 18 def upload_file(): 19 if request.method == 'POST': 20 file = request.files['file'] 21 if file and allowed_file(file.filename): 22 filename = secure_filename(file.filename) 23 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 24 return redirect(url_for('uploaded_file', 25 filename=filename)) 26 return render_template('upload.html') 27 28 if __name__=='__main__': 29 30 app.run('0.0.0.0',80,debug=True)
index.py
1 #!/usr/bin/env python 2 #coding=utf-8 3 4 from flask import Flask,render_template,request 5 6 app=Flask(__name__) 7 8 @app.route('/') 9 def index(): 10 #return "Hello,world!" 11 menu_list=[u'首页',u'简介',u'产品展示',u'视频教程',u'关于',u'联系我们'] 12 return render_template('home.html',menu_list=menu_list) 13 14 @app.route('/home/<username>') 15 def home(username): 16 return 'Your name is %s !' %username 17 18 @app.route('/page/') 19 def page(): 20 return 'Your name is page!' 21 22 @app.route('/about/') 23 def about(): 24 return 'About!' 25 26 @app.route('/login/',methods=['GET','POST']) 27 def login(): 28 if request.method=='POST': 29 print request.form['menuname'] 30 else: 31 print 'get' 32 return render_template('login.html') 33 34 with app.test_request_context(): 35 pass 36 37 if __name__=='__main__': 38 #app.debug=True 39 app.run('0.0.0.0',80)
home.html
1 <html> 2 <head> 3 <meta charset='utf-8'> 4 <title>Home</title> 5 </head> 6 <body> 7 <header> 8 <nav> 9 <ul> 10 {% for menu in menu_list%} 11 <li>{{ menu }}</li> 12 {% endfor %} 13 </ul> 14 </nav> 15 </header> 16 17 </body> 18 </html>
upload.html
1 <!doctype html> 2 <title>Upload new File</title> 3 <h1>Upload new File</h1> 4 <form action="" method=post enctype=multipart/form-data> 5 <input type=file name=file> 6 <input type=submit value=Upload> 7 </form>
login.html
1 <meta charset='utf-8'> 2 <title>login</title> 3 <div> 4 <form action="" method="post"> 5 <input type="text" name='menuname'> 6 <input type="submit" value="提交"> 7 </form> 8 </div>
sourct_filename.py
1 def secure_filename(filename): 2 r"""Pass it a filename and it will return a secure version of it. This 3 filename can then safely be stored on a regular file system and passed 4 to :func:`os.path.join`. The filename returned is an ASCII only string 5 for maximum portability. 6 7 On windows system the function also makes sure that the file is not 8 named after one of the special device files. 9 10 >>> secure_filename("My cool movie.mov") 11 'My_cool_movie.mov' 12 >>> secure_filename("../../../etc/passwd") 13 'etc_passwd' 14 >>> secure_filename(u'i contain cool \xfcml\xe4uts.txt') 15 'i_contain_cool_umlauts.txt' 16 17 The function might return an empty filename. It's your responsibility 18 to ensure that the filename is unique and that you generate random 19 filename if the function returned an empty one. 20 21 .. versionadded:: 0.5 22 23 :param filename: the filename to secure 24 """ 25 if isinstance(filename, text_type): 26 from unicodedata import normalize 27 filename = normalize('NFKD', filename).encode('ascii', 'ignore') 28 if not PY2: 29 filename = filename.decode('ascii') 30 for sep in os.path.sep, os.path.altsep: 31 if sep: 32 filename = filename.replace(sep, ' ') 33 filename = str(_filename_ascii_strip_re.sub('', '_'.join( 34 filename.split()))).strip('._') 35 36 # on nt a couple of special files are present in each folder. We 37 # have to ensure that the target file is not such a filename. In 38 # this case we prepend an underline 39 if os.name == 'nt' and filename and \ 40 filename.split('.')[0].upper() in _windows_device_files: 41 filename = '_' + filename 42 43 return filename
夕阳的进步与发展,靠各位同仁的支持与鼓励!谢谢!!!!