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.

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top