代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask import url_for
app = Flask(__name__)
print 'aa:',__name__
@app.route('/', methods=['GET', 'POST'])
def index():
print 'index'
return render_template('index.html')
@app.route('/user/' )
def user(name):
return 'Hello, {}!
'.format(name)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
print 'login'
print(request.method) #获取访问方式 GET
print(request.url) #获取url http://127.0.0.1:5000/req?id=1&name=wl
print(request.cookies) #获取cookies {}
print(request.path) # 获取访问路径 /req
print(request.args) #获取url传过来的值 ImmutableMultiDict([('id', '1'), ('name', 'wl')])
return redirect(url_for('index'))
return 'login!
'
if __name__ == '__main__':
print __name__
app.run(host='0.0.0.0', port=8000)
html 代码:
<!doctype html>
<html lang="en">
<head>
<title>Webville Tunes</title>
<meta charset="utf-8">
<link rel="icon"
type="image/ico"
href="http://wickedlysmart.com/favicon.ico">
<script>
window.onload = init;
function init() {
var button = document.getElementById("addButton");
button.onclick = handleButtonClick;
}
function handleButtonClick(e) {
var textInput = document.getElementById("songTextInput");
var songName = textInput.value;
alert("Adding " + songName);
if (songName == "") {
alert("Please enter a song");
}
else {
//alert("Adding " + songName);
var li = document.createElement("li");
li.innerHTML = songName;
var ul = document.getElementById("playlist");
ul.appendChild(li);
}
}
</script>
</head>
<body>
<form>
<input type="text" id="songTextInput" size="40" placeholder="Song name">
<input type="button" id="addButton" value="Add Song">
</form>
<ul id="playlist">
</ul>
</body>
</html>