1

Im trying to print html code that will also include variables, in this case its data from database with this code:

def displayHot():
myCursor = mongo.db.posts.find()
for result_object in myCursor:
        print''' 
        Content-Type: text/html

        <div class="card-header">
        <h3 %s
        </h3>
        </div>
        <div class="card-block">
        <p> %s 
        </p>
        <a class="panel-google-plus-image" href="{{url_for('Images', filename = 'Images/%s')}}"
        </a>
        </div" 
        ''' % (result_object['uploader'], result_object['description'], result_object['file_name'])

and rendering it here:

@app.route('/', methods=['POST','GET'])
def home():
    if request.method == 'GET':
        return render_template('index.html', function = displayHot())

when i run it, i get the printed data in my cmd.. so my question is how can i print the html code so it will act as html code and not just a print command in cmd

2
  • If you need to use something outside of a function, you need to return it, not print it. Commented Oct 8, 2016 at 3:28
  • Your indentation in displayHot function is wrong. Could you show your printed data and index.html? Commented Oct 8, 2016 at 4:51

1 Answer 1

3

It looks like you would be better served by doing your looping in a jinja template, and just sending the template the data for the loop. Something like this is what I'm thinking:

Python code

@app.route('/', methods=['POST','GET'])
def home():
    if request.method == 'GET':
        myCursor = mongo.db.posts.find()
        return render_template('index.html', cursor = myCursor)

index.html

<!-- your other code here -->
{% for result_object in cursor %}
    <div class="card-header">
        <h3> {{result_object['uploader']}}
        </h3>
    </div>
    <div class="card-block">
        <p> {{result_object['description']}}
        </p>
        <a class="panel-google-plus-image" href="{{url_for('Images', filename = '''Images/result_object['file_name']''')}}"
        </a>
    </div>
{% endfor %}
<!-- the rest of your html -->

This is simplified, and you would likely need to do more in order to get myCursor set up properly before sending it to the template, but I hope that gets the idea across.

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

Comments

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.