from flask import Flask,render_template,request
from others import checkLogin
app = Flask(__name__) #从templates里引用index.html
#return render_template("index.html")
@app.route('/')
def index():
return '''<form action="/login" method="post">
<input type="text" name="username" value=""><br >
<input type="text" name="password" value="">
<input type="submit" value="submit">
</form>
''' @app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'POST':
#checkLogin是自己写的使用mysql返回账号密码正确性,返回True/False
#request.form['username'] 从表单获取username的值,password同
#获取get参数的是 request.args.get['keyword']获取keyword参数值
if checkLogin(request.form['username'],request.form['password']):
return 'Hello '+request.form['username']
else:return 'Fail' if __name__ == '__main__':
#debug显示信息,host绑定ip,port 绑定端口
app.run(debug=True)

引用模板,并在模板显示信息

 from flask import Flask,render_template,request
app = Flask(__name__) @app.route('/user/<name>')
def index(name):
return render_template('user.html',username=name)

模板 user.html

  <ul>
{% if username %}
<li>{{ username }}</li>
{% endif %}
</ul>

用之前写的爬行wooyun镜像站实例(部分代码)记录:

 from flask import Flask,render_template,request
from wooyun import wooyunfilter
app = Flask(__name__) @app.route('/',methods=['GET'])
def index():
return render_template('index.html') @app.route('/search',methods=['GET'])
def search():
keyword = request.args.get('keyword')
title = keyword
obj = wooyunfilter(keyword,'')
a = obj.search()
pageZip = zip(a[0],a[1],a[2],a[3])
return render_template('wooyun.html',title=title,zip=pageZip) if __name__ == '__main__':
app.run(debug=True)

wooyun.html主要代码:

 <ul>
{% for hrefs,times,titles,types in zip %}
<li><strong>{{ times }}</strong><a href="{{ url_for('apear',next=hrefs) }}" target="_blank">{{ ' '+titles }}</a><span>{{ ' '+types }}</span></li>
{% endfor %} </ul>
05-11 14:03