【问题标题】:flask dynamic post url烧瓶动态帖子网址
【发布时间】:2016-12-20 20:04:07
【问题描述】:

我在我的 HTML 上构建了一个嵌套下拉菜单,并根据用户在嵌套下拉菜单中单击的内容,我希望它更改 WTF 表单中的默认值并将其发布到数据库。

所以我有多个不同的视图指向一个“帖子”视图。这些多个不同的视图是我嵌套下拉菜单中的不同菜单。

观看次数

@app.route('/first_drop_down')
def first_drop_down():
   return redirect('http://127.0.0.1:5000/post_something/first/')
@app.route('/second_drop_down')
def second_drop_down():
   return redirect('http://127.0.0.1:5000/post_something/second/')
@app.route('/post_something/<some_parameter>', methods=['GET','POST'])
def post_something(some_parameter)
   some_form = SomeForm()
   if some_parameter == 'first':
     some_form = SomeForm(field_a = some_parameter)
   elif some_parameter == 'second':
     some_form = SomeForm(field_a = some_parameter )
   if some_form.validate_on_submit():
     .
     .
     .
     .
     insert to db
     return redirect(url_for('index'))
   return render_template('user_input.html', some_paramter = some_paramter)

所以我遇到的问题是,在我在表单上按提交后,我一直收到 404。当在 post_something 视图上执行 get 请求时,url 是 /post_something/first/ 但是在按下提交按钮后,url 变为 /post_something// ,所以 url 对于 post 是空的,我猜这就是它抛出 404 的原因. 尽管我有点假设为什么会发生这种情况,但我有点不知道在发布时如何维护该 url。任何帮助将不胜感激。

【问题讨论】:

  • 我认为问题出在您的 HTML 代码中。你能用表单和下拉菜单发布它的一部分吗?

标签: python url flask


【解决方案1】:

来自flask api documentation,你会注意到 1. If a rule ends with a slash and is requested without a slash by the user, the user is automatically redirected to the same page with a trailing slash attached. 2. If a rule does not end with a trailing slash and the user requests the page with a trailing slash, a 404 not found is raised. 现在您已经定义了: @app.route('/post_something/<some_parameter>', methods=['GET','POST']) 你应该在表单中使用/post_something/first;或者使用url_for('post_something')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 2014-09-22
    • 2014-10-25
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多