烧瓶动态邮政网址

时间:2016-12-20 20:04:07

标签: python url flask

我在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视图上获取请求时,url是/ post_something / first /但是在按下提交按钮后,url会更改为/ post_something //,所以url为空以便发布,我猜这就是为什么它是投掷404。 即使我有点假设为什么会发生这种情况,但我仍然不知道如何在发布时维护该URL。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你会注意到来自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')代替。