Passing namedtuple attributes from a collection to an html file (Python/MongoDB)

424 Views Asked by At

This index.html view:

<head>
<title> {results.email}</title>
</head>
<body>
<h1> {results.company.name} <h1>
</body>              

I have the following collection (with embedded attributes) which I have instered in my MongoDB:

@cherrypy.expose
def create(self)
client = MongoClient()
db = client.database_name        
result = db.users.insert_one({
    "email": "[email protected]",
    "company": {
        "name": "ABC Company"
    }
})

After that I have stored these values using the namedtuple builtin function:

from collections import namedtuple
client = MongoClient()
db = client.database1        
cursor = db.users.find({"company.name":"ABC Company"})
company = namedtuple('company ', 'name') 
document = namedtuple('document ', 'email, company')
for row in cursor:
result = document(row["email"], company(row["company"]["name"]))

I would now like to pass these fields to my html index view file respecting the dot notation I created using the namedtuple function:

    template = open("index.html").read()
    return template.format(**????**)

How should pass the variables inside the format() function?

Comments: I'm using Python 2.7 version and MongoDB. I'm using cherryPy as HTTP server. I'm neither using an ORM nor a template engine.

1

There are 1 best solutions below

0
On BEST ANSWER

I've passed it to the view, like an object:

return template.format(result=result)