Capture more than one value from URL with Flask/Python
<p>I want to pass values to my Python script based on what button was clicked in the HTML. I can do it like this if there is only one value:</p>

<pre><code>@app.route('/', defaults={'category': ''})
@app.route('/&lt;category&gt;')
def index(category):
</code></pre>

<p>But altogether there are 3 values what I want to capture, and I can't figure out how to do that. All of these values are optional. One (a sorting method) has a default value set, the other two are not set by default.</p>

<p>How can I make this work within a single function and 3 variables? Thanks.</p>

<p>You can use <code>request.args</code> for query string parameters, which will be optional:</p>

<pre><code>@app.route('/')
def index():
    category = request.args.get('category', '')
    attr1 = request.args.get('attr1')
    attr2 = request.args.get('attr2')
</code></pre>

<p><code>request.args</code> is a dict (more exactly a MultiDict).</p>

<p>A request would look like this</p>

<p><code>/?category=my_category&amp;attr1=some_value&amp;attr2=some_value</code>
or</p>

<p><code>/?attr1=some_value</code></p>

<p><code>/</code></p>

python flask