之前静态页面中,是通过:
<a href="creatEvent.html" class="link icon-only" class="external"> <img src="{{ url_for(‘static’, filename=’img/avatar/navi_add_lightblue_24x24.svg’) }}"/> </a> |
实现页面跳转的到:
creatEvent.html
但是现在在Flask中,点击无效。
且希望,同时传递对应的参数user_openid
Flask jinja 页面跳转
Flask jinja jump page
Flask jinja redirect page
flask跳转页面参数加上#? – SegmentFault
jinja redirect page
jinja2 – Flask 手机版和PC版模板的代码设计问题 – SegmentFault
Flask 使用小结【Updating】 – leejun2005的个人页面 – 开源中国社区
redirect(url_for(‘foo’)) |
但是是在.py中实现的。
此处是需要在template的html中实现页面跳转
Simple Pagination | Flask (A Python Microframework)
[AF] Use Jinja to redirect to a page:flask
python – How can I "redirect" to another URL and pass a list object using Flask – Stack Overflow
->好像是:
template的html中,用:
url_for(“creatEvent")
然后在views.py中加上对应的路由,在路由中,render_templte
python – How do I create a link to another html page? – Stack Overflow
->果然是这个思路
但是还是不会传递参数啊
google app engine – Python jinja2 redirect with passing a parameter – Stack Overflow
jinja redirect url_for
python – Can’t pass a variable with Jinja in Flask – Stack Overflow
python – Flask url_for() with multiple parameters – Stack Overflow
python – Creating link to an url of Flask app in jinja2 template – Stack Overflow
然后此处,可以实现页面跳转了:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/templates/index.html
<a href="{{ url_for(‘creatEvent’, curUser = curUser) }}" class="link icon-only" class="external"> |
然后,对应的
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/views.py
@app.route(‘/creatEvent’, methods=[‘GET’, ‘POST’]) def creatEvent(): requestMethod = request.method app.logger.debug(‘requestMethod=%s’, requestMethod) requestArgs = request.args app.logger.debug(‘requestArgs=%s’, requestArgs) curUser = request.args.get("curUser", "") app.logger.debug(‘curUser=%s’, curUser) if request.method == ‘POST’: return redirect(url_for(‘index’)) else: return render_template("creatEvent.html", curUser = curUser) |
然后对于页面:
点击右上角的加号,可以接收到对应的参数,且是GET方法,
可以从对应的打印出来的log中看出来:
DEBUG in views [/root/html/SIPEvents/sipevents/views.py:298]: requestMethod=GET <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/html/SIPEvents/sipevents/views.py:300]: requestArgs=ImmutableMultiDict([(‘curUser’, u"<User nickname=u’\\u793c\\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>")]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/html/SIPEvents/sipevents/views.py:302]: curUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png> |
对应的跳转到新页面:
然后也是可以接收到对应的传入到参数:curUser对象的。
[总结]
Flask的Jinja中,从页面1,比如index.html,跳转到新页面页面2,比如createEvent,可以:
1.在template的html中,用:
<a href="{{ url_for(’someRounterInFlask’, parameter1 = value1) }}" > |
2.在对应的Flask的路由,比如views.py中,加上对应的路由
然后其中可以调用render_template,加上对应的页面的2的模板,就可以跳转页面了:
@app.route(‘/creatEvent’, methods=[‘GET’, ‘POST’]) def creatEvent(): requestMethod = request.method app.logger.debug(‘requestMethod=%s’, requestMethod) requestArgs = request.args app.logger.debug(‘requestArgs=%s’, requestArgs) value1 = request.args.get("parameter1", "") app.logger.debug(‘value1=%s’, value1) if request.method == ‘POST’: return redirect(url_for(‘index’)) else: return render_template("creatEvent.html", curUser = curUser) |
转载请注明:在路上 » [已解决]Flask的Jinja中如何实现页面跳转