1

I am trying to find how I can pass a URL parameter (not request) into the url_for function, but I cant find it. In the following example, I need to pass the q_id through the form, as I do with incrRight. So, how can I have variables in url_for in this scenario ?

<form action="{{url_for('answer',q_id='5495eb77433f064294361ceb')}}" method="post">
    <input type="text" name="incrRight" value="0">
    <input type="submit"/>
</form>

This is what I have on my controller:

@app.route('/api/v1.0/question/<string:q_id>/answer', methods = ['POST'])
def answer(q_id):
    incrRight = request.form.get('incrRight')
    . . .

I need my html form to be able to communicate with the above function, by passing a q_id and the incrRight parameter.

2
  • You mean query parameters as formed by the form? You'd have to change the method to GET in that case. Or do you mean in the POST body? Either way, you mean for the browser to include them as parameters? Commented Dec 20, 2014 at 22:35
  • The form will not produce '../answer/5495eb77433f064294361ceb?incrRight=<incrRight_value>'. It'll produce '../answer/5495eb77433f064294361ceb' with incrRight=<incrRight_value> being sent in the POST body, not the URL. Commented Dec 20, 2014 at 22:40

1 Answer 1

2

You can add additional form parameters by adding more <input> tags. If the user of the form is not supposed to changed the items, use a <input type="hidden"> element:

<form action="{{ url_for('answer') }}" method="post">
    <input type="text" name="incrRight" value="0" />
    <input type="hidden" name="q_id" value="5495eb77433f064294361ceb" />
    <input type="submit"/>
</form>

This does require that the answer() view can be invoked without the q_id parameter, of course:

@app.route('/api/v1.0/question/answer', methods=['POST'])
@app.route('/api/v1.0/question/<string:q_id>/answer', methods = ['POST'])
def answer(q_id=None):
    if q_id is None:
        q_id = request.form['q_id']
    incrRight = request.form.get('incrRight')

Here a missing q_id parameter in the URL signals to the view that the browser included the question id in the POST body instead.

Another option is for you to write JavaScript code that'll adjust the form action parameter based on the q_id field in the form, but this then requires that your visitors have JavaScript enabled.

Sign up to request clarification or add additional context in comments.

4 Comments

So, if the q_id is compulsory, I need to leave it in "{{ url_for('answer',q_id='dsad21') }}", and also add the input field which will overwrite the hardcoded value ?
@Giannis: no, the URL is used by your browser to know where to post the form to. Flask will then route to the right view. You'll have to adjust your view registration to accept a post without the q_id parameter.
@Giannis: you could put in a special code in the q_id field in the URL that you recognize as meaning look for the question id in the POST body instead; but in my version I used the absence of a the question id for that instead.
This is only my first API, but it feels that forcing the q_id as part of URL makes more sense, as 'answer' is an operation applied on a 'question'. The form is only used for the test page of the API, so maybe I should find a different way of testing this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.