Alex
Alex

Reputation: 3257

Variable is not shown in Flask template

I'm very new with templating engines and need help from someone smarter. Trying to generate searching results with Flask template, encountered lot of pain. Flask :

@app.route('/*?search', methods=['POST', 'GET'])
    if request.method == 'GET':
        if request.args.get('q'):

            qList = re.sub("[^\w]", " ",  request.args.get('q') ).split()

            htmlA = """
                    <div class="results" > 
                    <p>

                    """
            htmlB = """
                    </p>
                    <br></br>
                    </div>
                    """
            found = None
            for it in qList :
                found = htmlA + RESULT_BLAH_BLAH_METHOD( it ) + htmlB

            return render_template( 'list_of_found_items.html', found=found )

and html template part:

<somewhere in html>
    {% block content %}
        {{ found }}
    {% endblock %}
</somewhere in html>

With this, results are not shown on page even if are existing and checked with print to console output. What I've missed? Thank you.

Upvotes: 0

Views: 2078

Answers (1)

njzk2
njzk2

Reputation: 39406

autoescaping prevent from directly writing HTML in python. Your code could be re-written as:

qList = re.sub("[^\w]", " ",  request.args.get('q') ).split()
return render_template('list_of_found_items.html', items = map(RESULT_BLAH_BLAH_METHOD, qList))

And the following template

<somewhere in html>
    {% block content %}
        {% for item in qList %}
            <div class="results" >
                <p>{{item}}</p>
                <br></br>
            </div>
        {% endfor %}
    {% endblock %}
</somewhere in html>

Upvotes: 1

Related Questions