Discuss / Python / 请老师或同学们帮帮忙

请老师或同学们帮帮忙

Topic source

王新美男

#1 Created at ... [Delete] [Delete and Lock User]

我现在在登陆后,直接跳转到首页就是日志界面,没有问题。 但是,右上角不会显示刚刚注册的用户的name,还是显示注册、登陆。 在登陆后的result里面已经有用户的信息了,但是怎么将信息传递到主页的

__user__

里面呢。

我只能想到在

@get('/')
def index(*, page='1'):
    page_index = get_page_index(page)
    num = yield from Blog.findNumber('count(id)')
    page = Page(num)
    if num == 0:
        blogs = []
    else:
        blogs = yield from Blog.findAll(orderBy='created_at desc', limit=(page.offset, page.limit))
    return {
        '__template__': 'blogs.html',
        'page': page,
        'blogs': blogs,
        # '__user__':__user__
    }

在里面添加user主页右上角就可以显示用户名,但是这个方法里面的user的信息怎么获取呢?

请大家帮帮忙,我也是头疼好几天,一直卡在这了。

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

因为每个页面都需要用user,所以user在进入index()函数前就通过middleware绑定到request对象了:

https://github.com/michaelliao/awesome-python3-webapp/blob/day-16/www/app.py#L63

在response handler里面把request.__user__取出来放到dict中:

https://github.com/michaelliao/awesome-python3-webapp/blob/day-16/www/app.py#L111


  • 1

Reply