Why my strange results rendering the user object?

喜你入骨 提交于 2019-12-13 07:11:35

问题


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:


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


来源:https://stackoverflow.com/questions/7735502/why-my-strange-results-rendering-the-user-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!