Question

I have a python function in modules in web2py to send e-mails.It has the following code

message = response.render('scheduler/connectionmessage.html',cont)

I get the error

<type 'exceptions.NameError'> name 'response' is not defined"

How can I make render available in modules? The objective is to have a few such scripts under modules and execute them via scheduler from a stub under controllers. More code -

def send_email_invites():
  from gluon import *
  from gluon.template import render 
  db = current.db
  ......<execute query and populate dictionary>
  message = response.render('scheduler/connectionmessage.html',cont)

That is about it.

Was it helpful?

Solution

Your code already includes from gluon import *, which means you have imported the thread local current object. That object includes the response object for the current request, so you should refer to current.response rather than just response.

Note, this is not necessary in model, controller, and view files because those files are executed in a global environment that already includes the response object (along with much of the rest of the web2py API).

For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.

OTHER TIPS

Try this before calling response.render()

from gluon.globals import Response 
response = Response()

And I know it's tempting, but try to avoid from xyz import * and be explicit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top