Python Flask返回带有参数的重定向,而不在url中显示参数

时间:2018-07-13 13:41:15

标签: python url redirect flask url-for

我有一个简单的python flask应用程序。当我的一位用户在我的主页上发表评论时,我想将他们重定向回主页。目前,我拥有处理主页和评论的相同路线。看起来像这样:

@app.route('/home', methods=['POST', 'GET'])
@login_required
def home():
    form = CommentForm()
    user = User.query.filter_by(username=current_user.username).first()
    house = House.query.filter_by(owner_id = current_user.id).first()
    neighbors = Neighbors.query.filter_by(houseid=house.id).all()
    if user.id == current_user.id:
        if form.validate_on_submit():
            time = strftime("%a, %b %d %Y %X", localtime())
            comment = Commentsection(houseid = house.id, commentbody = form.body.data, times = str(time), commenter=user.username, commenterid=user.id, commenterpic = user.image_file )
            db.session.add(comment)
            db.session.commit()
            link = url_for('add', invite_token=house.linkinv, _external=True)
            comments = Commentsection.query.filter_by(houseid=house.id).order_by(Commentsection.id.desc()).all()
            return redirect(url_for('home', title='Home', house=house, form=form, comments=comments, link=link, neighbors=neighbors))
        else:
            link = url_for('add', invite_token=house.linkinv, _external=True)
            comments = Commentsection.query.filter_by(houseid=house.id).order_by(Commentsection.id.desc()).all()
            return render_template('home.html', tite='Home', house=house, form=form, comments=comments, link=link, neighbors=neighbors)

当用户提交评论时,将运行代码并验证表单。表单通过验证后,将创建注释模型并将其提交到数据库。之后,它将用户重定向到同一页面,以避免chrome中出现讨厌的“您想再次提交此表单”弹出窗口。如果我没有重定向,则每次我重新加载页面时,表单都会继续提交。问题是,当我重定向时,url_for认为我正在传递url参数而不是变量,并将它们直接放入url中,结果如下:

http://127.0.0.1:5000/home?`title=Home&house=House%28%27Nicks+Home%27%2C%2732+Ellridge+Place%27%2C%271%27%2C%29&form=%3Cdog_house.forms.CommentForm+object+at+0x04A75C10%3E&comments=Commentsection%28%2710%27%2C%271%27%2C%27Hello+world%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%279%27%2C%271%27%2C%27Hello+world%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%277%27%2C%271%27%2C%27Hello+world%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%276%27%2C%271%27%2C%27Nickkk%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%275%27%2C%271%27%2C%27Hello+man%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%274%27%2C%271%27%2C%27Nick%3F%27%2C%27nickdeb%27%2C%27default.jpg%27%2C%29&comments=Commentsection%28%273%27%2C%271%27%2C%27Hey+nick%27%2C%27nick%27%2C%27414f2185a17700081ecfff3c948527405e44c507f029d068.JPG%27%2C%29&comments=Commentsection%28%272%27%2C%271%27%2C%27Hello+world%27%2C%27nick%27%2C%27414f2185a17700081ecfff3c948527405e44c507f029d068.JPG%27%2C%29&comments=Commentsection%28%271%27%2C%271%27%2C%27Hello+world%27%2C%27nick%27%2C%27414f2185a17700081ecfff3c948527405e44c507f029d068.JPG%27%2C%29&link=%2Fhome&neighbors=%3CNeighbors+1%3E`

代替:

http://127.0.0.1:5000/home

如您所见,该URL与第二个URL相比非常丑陋,并且可能会暴露用户数据。 变量仍被传递到html模板。这可能吗?

TLDR;将参数和变量传递到重定向(url_for),而无需将变量直接传递到URL。

0 个答案:

没有答案