flask 网页 javascript 按钮 点击事件

代码:

#!/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/<name>')
def user(name):
    return '<h1>Hello, {}!</h1>'.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 '<h1>login!</h1>'

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>



效果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值