我无法从mongodb查询数据并渲染到Flask

时间:2017-11-20 04:23:21

标签: python mongodb flask pymongo

我知道MongoDB中存在数据,因为我可以在外部查询我的Flask应用程序。

查询:

from pymongo import MongoClient
import pprint
client = MongoClient()
db = client.reqdata

cursor = db.req_data.find()

for document in cursor:
    pprint.pprint(document)

示例结果

{'_id': ObjectId('5a0f3abf2d6a8810fbc8e352'),
 'req': {'HiringManagerEmailId': 'Fred.Smith@company.com',
         'HiringManagerEmployeeId': 353585,
         'HiringManagerName': 'Fred Smith',
         'HiringManagerSystemId': 146472,
         'HotJob': 'No',
         'JobDescription': 'You will collaborate with colleagues from '
                           'North America and/or The Company '
                           'on national and international '
                           'client projects.<br> <br>Together with our '
                           'clients, you will develop superior IT concepts '
                           'and architecture solutions as well as support '
                           'technical implementations actively and on site, '
                           'applying your sound technical know-how, your '
                           'understanding of business contexts, and your '
                           'analytical and conceptual '
                           'skills.<br><br>Company provides technology '
                           'consulting services to clients on the following '
                           'topics:<br> <ul dir="ltr"><li> Management of '
                           'large-scale projects</li><li> Restructuring of '
                           'IT processes and organization</li><li> '
                           'Functional and technical support of mergers and '
                           'acquisitions</li><li> Analysis of IT and '
                           'software architecture weaknesses</li><li> '
                           'Specification and implementation of IT '
                           'architectures</li><li> Product evaluation, such '
                           'as core systems</li><li> Technical concept '
                           'development and design of software '
                           'solutions</li><li> Quality assurance for IT '
                           'implementation</li><li> Design and '
                           'implementation of feasibility studies</li><li> '
                           'Load and performance tests</li></ul>',
         'JobDetailLink': 'https://someurl.foo.com/',
         'LastUpdated': '16-Nov-2017',
         'Question': ['Senior Consultant',
                      'Core Technology',
                      'North America',
                      'United States',
                      'New York',
                      'Full time',
                      '5390BR'],
         'RecruiterEmailId': 'Fred.Smith@company.com',
         'RecruiterEmployeeId': 353585,
         'RecruiterName': 'Fred Smith',
         'RecruiterSystemId': 146472},
 'timestamp': '2017-17-17 14:38:37'}

当我尝试将相同的代码插入我的Flask应用程序时,我可以准确计算呈现到〜/ index页面的文档,但似乎无法将数据呈现到〜/ mongoreqs页。

Flask Code

from flask import Flask, render_template,request,redirect,url_for # For flask implementation
from pymongo import MongoClient # Database connector
from bson.objectid import ObjectId # For ObjectId to work

client = MongoClient()    #Configure the connection to the database
db = client.reqdata    #Select the database
##reqs = db.req_data.find()

app = Flask(__name__)
title = "Req Search with Flask"
heading = "Req Search"



@app.route('/')
@app.route('/index')

def index():
##    reqdb = db.req_data
    reqcount = db.req_data.count()
    return render_template('index.html',
                          title='Home',
                           reqcount = reqcount)

@app.route('/mongoreqs', methods=['GET'])
def mongo_reqs():
##    reqdb = db.req_data #Select the collection
##    client = MongoClient()    #Configure the connection to the database
##    db = client.reqdata 
    reqs = db.req_data.find({})
    return render_template('mongoreqs.html',
                           title="Reqs in Mongo DB",
                           reqs=reqs)


if __name__ == "__main__":
    app.run(debug=True)

mongoreqs.html

<!-- extend from base layout -->

{% extends "base.html" %}


{% block content %}

    <h2>Open Requisitions MONGO DB </h2>
    <table>
    <th><strong>Question</strong></th>
    <th><strong>URL </strong></th>
    <th><strong>Job Description</strong></th>

    {% for req in reqs %}
            <tr>
            <td>{{ req["Question"] }}</td>
            <td><a href="{{ req['JobDetailLink'] }}" target="blank">Job Apply Link</a></td>
            </th><td>{{ req['JobDescription']|safe }}</td>

        </tr>
    {% endfor %}
    </table>
{% endblock %}

非常感谢任何想法或帮助。

Screen Shot of Output

1 个答案:

答案 0 :(得分:1)

从文档的漂亮字体中,信息嵌套在req

在模板中,使用:

{{ req.req.Question }}

{{ req.req.JobDetailLink }}    

{{ req.req.JobDescription }}

或者更好的是仍然可以将投影写入不需要的属性,然后就可以按原样使用当前模板。

projection = {
    'Question': '$req.Question',
    'JobDetailLink': '$req.JobDetailLink',
    'JobDescription': '$req.JobDescription',
}
cursor = db.find({}, projection)