Why my strange results rendering the user object?

2019-09-04 04:31发布

For some reason, my variable {{current_user.name}} doesn't display anything and the variable {{username}} displays instead of Niklas R it displays `(u'Niklas R',) on the page that I render with django. Here is the method from the basehandler that I hope you can tell me what's wrong with:

def render(self, name, **data):
    """Render a template"""
    if not data:
        data = {}
    data[u'js_conf'] = json.dumps({
        u'appId': facebookconf.FACEBOOK_APP_ID,
        u'canvasName': facebookconf.FACEBOOK_CANVAS_NAME,
        u'userIdOnServer': self.user.user_id if self.user else None,
    })
    data[u'logged_in_user'] = self.user
    data[u'message'] = self.get_message()
    data[u'csrf_token'] = self.csrf_token
    data[u'canvas_name'] = facebookconf.FACEBOOK_CANVAS_NAME
    logging.debug('user object: '+str(self.current_user))
    data[u'current_user']=self.current_user,
    data[u'username']=self.current_user.name,
    data[u'facebook_app_id']=FACEBOOK_APP_ID
    self.response.out.write(template.render(
        os.path.join(
            os.path.dirname(__file__), 'templates', name + '.html'),
        data))

1条回答
Anthone
2楼-- · 2019-09-04 05:06

This happens to me all the time, you have an extra , and the variable becomes a tuple.

change the lines to:

data[u'current_user']=self.current_user
data[u'username']=self.current_user.name
查看更多
登录 后发表回答