I'm currently using Pylons 0.9.5 with Python 2.4

I solved the Unicode problems with Mako, so let me tell you what I did
(for those that have searched countless forums as I have done).  Then
I'll mention what seems to be a hack that I would like to get cleared
up....


How to get unicode working with Pylons+Mako:

--------------------------------------------------------------------
config/environment.py:
--------------------------------------------------------------------
    [...snippet below...]

    # Add your own template options config options here, note that all
config options will override
    # any Pylons config options
    tmpl_options['mako.imports'] = ['from webhelpers import auto_link
as l',
                                    'from webhelpers import
simple_format as s',
                                    'from rosetta.lib import
myfilters',
                                    'from paste.deploy import CONFIG']

    # ~~~~BELOW SOLVES THE UNICODE PROBLEM FOR TEMPLATES ~~~~
    # Give support for Unicode in Mako templating system
    # from: http://wiki.pylonshq.com/pages/viewpage.action?pageId=5439551
    tmpl_options['mako.input_encoding'] = 'utf-8'
    tmpl_options['mako.output_encoding'] = 'utf-8'

    # convert request.params to Unicode automatically
    # Pylons 0.9.6 does this automatically, but we are in 0.9.5 still
so it must be explicit
    #return pylons.config.Config(tmpl_options, map, paths)
    # Return our loaded config object
    return pylons.config.Config(tmpl_options, map, paths,
request_settings=dict(charset='utf-8', errors='replace'))

--------------------------------------------------------------------
models/__init__.py:
--------------------------------------------------------------------
    [...snippet below...]
def init_model(app_conf):
    """
    Setup the model.
    This gets called by lib.app_globals
    """

    print "Initializing model."
    uri = app_conf['sqlalchemy.default.dburi']

    # ~~~~BELOW SOLVES THE UNICODE PROBLEM FOR YOUR MODEL ~~~~
    # use: encoding='utf-8', convert_unicode=True to convert all
unicode at the database level so its unicode going into python/mako
    engine = create_engine(uri, pool_recycle=14400, echo_pool=True,
encoding='utf-8', convert_unicode=True)
    meta.connect(engine)
    meta.engine.echo = asbool(app_conf.get('sqlalchemy.default.echo',
'false'))





That's it!!


Ok to the ^^^QUESTION^^^...

I have unicode working great, but for some reason when using the Auth
plugin and the webhelpers url_for method I still need to explicitly
encode the variable as utf-8.  This seems a bit hacky though.  Anyone
else get into a similar situation and/or have a more elegant solution
(or am I just doing something wrong here)?



Here's a snippet of the code I'm using for that:

--------------------------------------------------------------------
snippet for Auth problem
--------------------------------------------------------------------
    def signin_method(self):
        uname = request.params.get('username',
'').strip().encode('utf-8', 'replace')      <----- throws a
UnicodeDecodeError if not first doing an encode
        request.environ['paste.auth_tkt.set_user'](uname)

--------------------------------------------------------------------
url_for problem
--------------------------------------------------------------------
${ h.url_for('page', nickname=notebook.user.nickname,
notebook_id=notebook.book_id, highlight = c.query.encode('utf-8',
'replace')

where highlight becomes a param so the url would look something like:
/users/me/1/?highlight=úñicode_phrase



thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to