WebFaction
Community site: login faq

I'm getting the following error from my cherrypy script that is being generated by the submit module.

ValueError: Page handlers MUST return bytes. Use tools.encode if you wish to return unicode

I turned tool.encode on in my config but I'm still getting this error. I'm allowing users to upload content via the jQuery Form Plugin. Anythoughts as to why I'm getting this error?

Here is my cherrypy file:

class Root(object):

@cherrypy.expose
def index(self)
    return open('/home/joestox/webapps/freelinreg_static/index.html')

@cherrypy.expose
def submit(self, myfile):

    cherrypy.session['myfile'] = myfile
    data_name = myfile.filename

    #Send back to JQuery with Ajax
    #Put in JSON form
    data_name= json.dumps(dict(title = data_name))
    cherrypy.response.headers['Content-Type'] = 'application/json'

    return data_name

cherrypy.config.update({
    'tools.staticdir.debug': True,
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': *****,
    'tools.sessions.on': True,
    'tools.encode.on': True,
    'tools.encode.encoding': 'utf-8',
})

config = {
}

cherrypy.tree.mount(Root(), '/', config=config)
cherrypy.engine.start()

HTML:

<!DOCTYPE html>
    <html>
        <head> 
            <script type='text/javascript' src='freelinreg_static/google.js'></script>
            <script type='text/javascript' src='freelinreg_static/frontend.js'></script>
            <script type='text/javascript' src='freelinreg_static/malsup.js'></script>
        </head>
        <body>

        <form id="dataform" action="submit" method="post" enctype="multipart/form-data">
            <input type="file" name="myfile" id="myFile"/>
            <input type="submit" id="data_submit" value="Continue"/>
        </form>

        </body>
    </html>

jQuery (frontend.js):

$(document).ready(function () {
    (function () {
        $('#dataform').ajaxForm({
            url: "submit",
            success: function (data) {
                var $a_var = data['title'];
                $('body').append($a_var);
            }
        });
        return false;
    })();
});

asked 26 Nov '13, 04:56

joestox
3713
accept rate: 0%


The solution to the problem was to change this line...

return data_name

...to this...

return data_name.encode('utf8')

The reason for this is because json.dumps always produces unicode data (not "bytes"), and so you need to convert the unicode to bytes with the encode function.

Regards,

~Christopher S, WebFaction Support

permanent link

answered 26 Nov '13, 18:51

likebike
3366
accept rate: 28%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×32
×19
×7

question asked: 26 Nov '13, 04:56

question was seen: 4,513 times

last updated: 26 Nov '13, 18:51

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM