Antespo
Antespo

Reputation: 149

How to get data from a form to jinja2

I am trying to get a value inputted through a form and then back into my jinja template. Which I know doesn't make sense so I guess I am asking how do I go about doing what I want?. Here is what I have:

Python

@app.route('/test', methods=['GET', 'POST'] )
    def test():
    posts = db.posts
    facultyId = request.form.get("facultyId","");
    print "facultyId: ",facultyId
    return render_template('form.html',posts=posts,Id=facultyId)

form.html

<form method='post'>
        <table width="80%" border="5" align="center" bgcolor="white">       
            <tbody>
                <tr>
                    <th colspan= "4">
                            Faculty Identification Number: 
                            <input type="text" id="facultyId" name="facultyId" value=""/>

                    </th>       
                </tr>
                    <tr>
                    <th colspan= "4">
                        Number Of Evaluations: 
                            {% if posts.find({"Applicants.appId" : Id},{'Applicants.Evaluators':{'$exists': True }}).count() == 0 %}
                                {{posts.find({"Applicants.appId" : Id},{'Applicants.Evaluators':{'$exists': True }}).count() }}
                            {% else %}
                                {% for post in posts.find({"Applicants.appId" : Id}, { "Applicants.$.Evaluators" : 1 }) %}
                                    {{post["Applicants"][0]["Evaluators"]|length}}
                                {% endfor %}
                            {% endif %}
                    </th>
                    </tr>
                    <th colspan= "4"><button type="submit" >Submit</button></th>

                </tbody>
            </table>
</form>

I want to be able to submit a facultyId though a form and have it go into my jinja and run my mongodb find query. It works if I hard code the value in so if I did Id=100 in my python it works but if I do it though the forums it doesn't and the facultyId value is getting inputted because it does prints out.

Upvotes: 2

Views: 20474

Answers (4)

juzten
juzten

Reputation: 154

I would also suggest to put your posts.find logic into your route function, then pass it's results to your form in the posts variable.

Upvotes: 0

Try to set action controller as follows

<form method='post' action='/test'>

Upvotes: 1

Bartosz Marcinkowski
Bartosz Marcinkowski

Reputation: 6881

I think the problem is that you do not parse facultyId as an integer. It works if you hardcode 100, because it is an integer, but what you assign out of request.form is a string "100".

After

facultyId = request.form.get("facultyId","")

add

facultyId = int(facultyId) if facultyId else None

Upvotes: 0

Stephen Lin
Stephen Lin

Reputation: 4912

Try to put it in braces. Like this:

<form method='post'>
        <table width="80%" border="5" align="center" bgcolor="white">       
            <tbody>
                <tr>
                    <th colspan= "4">
                            Faculty Identification Number: 
                            <input type="text" id="facultyId" name="facultyId" value=""/>

                    </th>       
                </tr>
                    <tr>
                    <th colspan= "4">
                        Number Of Evaluations: 
                            {% if posts.find({"Applicants.appId" : {{Id}}},{'Applicants.Evaluators':{'$exists': True }}).count() == 0 %}
                                {{posts.find({"Applicants.appId" : {{Id}}},{'Applicants.Evaluators':{'$exists': True }}).count() }}
                            {% else %}
                                {% for post in posts.find({"Applicants.appId" : {{Id}}}, { "Applicants.$.Evaluators" : 1 }) %}
                                    {{post["Applicants"][0]["Evaluators"]|length}}
                                {% endfor %}
                            {% endif %}
                    </th>
                    </tr>
                    <th colspan= "4"><button type="submit" >Submit</button></th>

                </tbody>
            </table>
</form>

Upvotes: 0

Related Questions