HelloWorld

[Flask]学习Flask第三天笔记总结

 1 from flask import Flask,render_template,request
 2 from others import checkLogin
 3 app = Flask(__name__)
 4 
 5 #从templates里引用index.html
 6 #return render_template("index.html")
 7 @app.route('/')
 8 def index():
 9     return '''<form action="/login" method="post">
10                 <input type="text" name="username" value=""><br >
11                 <input type="text" name="password" value="">
12                 <input type="submit" value="submit">
13                 </form>
14            '''
15 
16 @app.route('/login',methods=['GET','POST'])
17 def login():
18     if request.method == 'POST':
19         #checkLogin是自己写的使用mysql返回账号密码正确性,返回True/False
20         #request.form['username'] 从表单获取username的值,password同
21         #获取get参数的是 request.args.get['keyword']获取keyword参数值
22         if checkLogin(request.form['username'],request.form['password']):
23             return 'Hello '+request.form['username']
24         else:return 'Fail'
25 
26 if __name__ == '__main__':
27     #debug显示信息,host绑定ip,port 绑定端口
28     app.run(debug=True)

 

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

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

模板 user.html

1  <ul>
2      {% if username %}
3          <li>{{ username }}</li>
4      {% endif %}
5  </ul>

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

 1 from flask import Flask,render_template,request
 2 from wooyun import wooyunfilter
 3 app = Flask(__name__)
 4 
 5 
 6 @app.route('/',methods=['GET'])
 7 def index():
 8     return render_template('index.html')
 9 
10 @app.route('/search',methods=['GET'])
11 def search():
12     keyword = request.args.get('keyword')
13     title = keyword
14     obj = wooyunfilter(keyword,'')
15     a = obj.search()
16     pageZip = zip(a[0],a[1],a[2],a[3])
17     return render_template('wooyun.html',title=title,zip=pageZip)
18 
19 if __name__ == '__main__':
20     app.run(debug=True)

wooyun.html主要代码:

1 <ul>
2        {% for hrefs,times,titles,types in zip %}
3        <li><strong>{{ times }}</strong><a href="{{ url_for('apear',next=hrefs) }}" target="_blank">{{ '   '+titles }}</a><span>{{ '   '+types }}</span></li>
4        {% endfor %}
5 
6 </ul>

 

posted @ 2016-09-21 03:08  Loid  阅读(339)  评论(0编辑  收藏  举报