Adding URL parameters to form action in flask

Question:

Is there a way to pass an additional list of parameters outside of a form when the form is submit?

I know I can create a new hidden input field in the form for each parameter I want to pass through the form, but is there another way? I tried to add parameters directly to the form action but that doesn’t work. The code in its entirety can be found here:

from flask import Flask, request
import os

app = Flask(__name__)

@app.route('/')
def index():
  print request.args
  return "<form action='/?foo=bar'><input name='variable' type='text' /></form>"

if __name__ == '__main__':
  port = int(os.environ.get('PORT', 8080))
  app.run('0.0.0.0', debug=True, port=port)

Clarifying question based on feedback from comments:

Using this code, when I type "123" into the textbox and hit enter, I see that the url is http://my-workspace.c9users.io/?variable=123.

What I want is http://my-workspace.c9users.io/?foo=bar&variable=12‌​3.

Asked By: user823447

||

Answers:

I think you should show what you input and what you want output.

In your code, the index() function default accept the GET method, so if you want to post data from form add the methods to the @app.route().

@app.route('/', methods=['GET', 'POST'])

and, the form also should add post method.

<form method='post', action='/'></form>
Answered By: agnewee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.