最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Jinja在form的POST时给url_for时传递的参数后Flask中路由中获取不到

Flask crifan 5380浏览 0评论

Jinja的template的html

<form action=”{{ url_for(“creat_event”, curUserOpenid=currentUser.openId) }}” method=”POST”>

但是在Flask的views.py中:

@app.route(‘/creat_event’, methods=[‘GET’, ‘POST’])
def creat_event():
    requestMethod = request.method
    app.logger.debug(‘requestMethod=%s’, requestMethod)
    requestArgs = request.args
    app.logger.debug(‘requestArgs=%s’, requestArgs)
    #curUser = request.args.get(“curUser”, None)
    #curUser = request.args.get(“curUser”, User(openid=””))
    #app.logger.debug(‘curUser=%s’, curUser)
    #app.logger.debug(‘type(curUser)=%s’, type(curUser))
    curUserOpenid = request.args.get(“curUserOpenid”, “”)
    app.logger.debug(‘curUserOpenid=%s’, curUserOpenid)
    app.logger.debug(‘type(curUserOpenid)=%s’, type(curUserOpenid))

却获取不到参数:

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:315]:
requestMethod=POST

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:317]:
requestArgs=ImmutableMultiDict([(‘curUserOpenid’, u”)])

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:323]:
curUserOpenid=

去改为:

<form action=”{{ url_for(“creat_event”, curUserOpenid=”abcdefg”) }}” method=”POST”>

结果也还是收不到。

form submit POST url_for

python – Issue a POST request with url_for in Flask – Stack Overflow

-》只支持GET

不支持POST。。。

-》只能是submit按钮提交GET

或者是AJAX去提交POST

form   POST url_for

python – GETting information and POSTing a form in Flask – Stack Overflow

Flask web开发 处理POST请求(登录案例) – 51kata – 博客园

Flask HTTP methods

算了,还是放到form中吧。。

<form action=”{{ url_for(“creat_event”) }}” method=”POST”>
    <input type=”text” hidden=”true” name=”curUserOpenid” value=”{{ currentUser.openId }}”/>

 和:

curUserOpenid = “”
if request.method == ‘POST’:
    curUserOpenid = request.form.get(“curUserOpenid”, “”)
else:
    curUserOpenid = request.args.get(“curUserOpenid”, “”)
app.logger.debug(‘curUserOpenid=%s’, curUserOpenid)
app.logger.debug(‘type(curUserOpenid)=%s’, type(curUserOpenid))

然后还是不行。

最后的最后,确定了:

此处是由于自己的笔误:

把错误的:

<form action=”{{ url_for(“creat_event”, curUserOpenid=curUser.openId) }}” method=”POST”>

改为:

<form action=”{{ url_for(“creat_event”, curUserOpenid=curUser.openid) }}” method=”POST”>

最后就可以正常收到参数:

curUserOpenid

了。

 

[总结]

Jinja在模板的html中,form在POST时候,通过的url_for中的参数,是可以正常传递参数的,比如:

<form action=”{{ url_for(“creat_event”, curUserOpenid=curUser.openid) }}” method=”POST”>

即可在Flask的views.py中的路由:

@app.route(‘/creat_event’, methods=[‘GET’, ‘POST’])
def creat_event():
    requestMethod = request.method
    app.logger.debug(‘requestMethod=%s’, requestMethod)
    requestArgs = request.args
    app.logger.debug(‘requestArgs=%s’, requestArgs)
    #curUser = request.args.get(“curUser”, None)
    curUserOpenid = request.args.get(“curUserOpenid”, “”)
    app.logger.debug(‘curUserOpenid=%s’, curUserOpenid)
    app.logger.debug(‘type(curUserOpenid)=%s’, type(curUserOpenid))

中,接收到参数:

<div–<——————————————————————————
DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:315]:
requestMethod=POST

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:317]:
requestArgs=ImmutableMultiDict([(‘curUserOpenid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)])

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:330]:
curUserOpenid=oswjmv4X0cCXcfkIwjoDfCkeTVVY

转载请注明:在路上 » [已解决]Jinja在form的POST时给url_for时传递的参数后Flask中路由中获取不到

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.199 seconds, using 22.11MB memory