app .py
from flask import Flask,render_template,request,redirect,url_for,make_response,abort
from flask_bootstrap import Bootstrap
from werkzeug.routing import BaseConverter
from werkzeug.utils import secure_filename
from flask_script import Manager
from os import path
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter,self).__init__(url_map)
self.regex = items[0]
app = Flask(__name__)
app.url_map.converters['regex'] = RegexConverter
app.config['DEBUG'] = True
manager = Manager(app)
bootstrap = Bootstrap(app)
@app.route('/')
def hello_world():
response = make_response(render_template('hello.html',line1='eaa'))
response.set_cookie('username', '')
return response
@app.route('/services')
def services():
return 'Services'
@app.route('/about')
def about():
return 'About'
@app.route('/user/<regex("[a-z]{3}"):user_id>')
def user(user_id):
return 'User %s' % user_id
@app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
else:
# http://127.0.0.1:5000/login?username=ray
username = request.args['username']
return render_template('login.html',method=request.method)
@app.route('/upload',methods=['GET','POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
basepath = path.abspath(path.dirname(__file__))
upload_path = path.join(basepath, 'static', 'uploads', secure_filename(f.filename))
f.save(upload_path)
return redirect(url_for('upload'))
return render_template('upload.html')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
@manager.command
def dev():
from livereload import Server
live_server = Server(app.wsgi_app)
live_server.watch('**/*.*') # 监测整个项目所有文件变化。可以加参数,不加就整个项目重加载,加的话就执行方法里的内容。
live_server.serve(open_url=True)
if __name__ == '__main__':
manager.run()
# app.run(debug=True)
hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="{{ url_for('static', filename='site.css') }}" />
</head>
<body>
<h1>{{ line1 }}</h1>
<nav>
<a href="{{ url_for('.services') }}">Services</a>
<a href="{{ url_for('.about') }}">About</a>
</nav>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>HTTP 方法:{{ method }}</h1>
<form method="post">
<div>
<input type="text" placeholder="username">
</div>
<div>
<input type="password" placeholder="password">
</div>
<input type="submit">
</form>
</body>
</html>
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>文件上传示例</h1>
<form action=""
method=post
enctype="multipart/form-data">
<p><input type="file" name="file">
<input type="submit" value="Upload"></p>
</form>
</body>
</html>
404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>404 </h1>
<h2>很抱歉!</h2>
<p>您访问的页面并不存在</p>
</body>
</html>
site.css
body{
color:#efefef;
}
a,a:visited{
color:#000;
}
生成requirements.txt
pip freeze > requirements.txt
pip install -r requirements.txt