Beispiel #1
0
def index(request):
    """
    todo 首页的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.title, edit_link,
                                            delete_link)
        todos.append(s)
    todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #2
0
def index(request):
    """
    todo 首页的路由函数
    """
    log('我要 request', request)
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        created_time = t.created_time
        updated_time = t.updated_time
        log('created_time', t.created_time, created_time)
        s = f'<h3>{t.id} {t.title} {created_time}: {updated_time} {edit_link} {delete_link}</h3>'
        todos.append(s)
    todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #3
0
def index(request):
    """
    _todo 首页的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录用户,如果没有登录,就 redirect 到 /login
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    # 下面这一行 生成一个html字符串
    # todo_html = ''.join(['<h3>{}: {}</h3>'.format(t.id, t.title)
    #                      for t in todo_list])

    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        create_time = '创建时间: {}\t'.format(t.create_time)
        update_time = '<br />更新时间: {}\t'.format(t.update_time)
        s = '<h3>{} : {} {} {}</h3><h5>{} {}</h5>'.format(
            t.id, t.title, edit_link, delete_link, create_time, update_time)
        todos.append(s)
    todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    # 下面这三行可以改写为一条函数, 还把 headers 也放进 函数中
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #4
0
def index(request):
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    body = template('jinja_test.html', user_list=todo_list)
    log('这是 body:', body)
    return response_http(body)
Beispiel #5
0
def index(request):
    """
    /todo 页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    # 如果已经登录,那么获取这个登录用户下面的所有数据
    todo_list = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    todos = []
    # 遍历当前用户下的所有数据
    for t in todo_list:
        # 每一个数据后面都跟着一个编辑和删除的链接
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        # 把用户的每一条数据的id、标题title、编辑数据链接、删除数据链接放入todos这个list中
        # 比如,['<h3>3 : 喝水 <a href="/todo/edit?id=3">编辑</a> <a href="/todo/delete?id=3">删除</a></h3>',...]
        s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.title, edit_link,
                                            delete_link)
        todos.append(s)
        # log('todos.append(s) 是什么 -> ', todos)
    # 把todos这个list做join操作,得到比如
    # '<h3>3 : 喝水 <a href="/todo/edit?id=3">编辑</a> <a href="/todo/delete?id=3">删除</a></h3><h3>....</h3> ...'
    todo_html = ''.join(todos)
    # log('todo_html -> ', todo_html)
    body = template('todo_index.html')
    # 替换todo_index.html模板文件中的{{todos}}标记字符串为todo_html
    body = body.replace('{{todos}}', todo_html)
    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    # headers上面已经设置过了, headers = {'Content-Type': 'text/html',}
    # 返回请求头, HTTP/1.1 200 VERY OK\r\nContent-Type: text/html\r\n
    header = response_with_headers(headers)
    # 把请求头和body拼接
    """
    HTTP/1.1 200 VERY OK\r\n
    Content-Type: text/html\r\n\r\n
    <html>
        .........
    </html>
    
    """
    r = header + '\r\n' + body
    log('4.服务器给浏览器一个页面响应', r)
    return r.encode(encoding='utf-8')
Beispiel #6
0
def index(request, u):
    """
    todo 首页的路由函数
    """

    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    # uname = current_user(request)
    # u = User.find_by(username=uname)
    # if u is None:
    #     return redirect('/login')
    todos = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    return http_response(template_render('todo_index.html', todos=todos))
Beispiel #7
0
def index(request):
    """
    todo 首页的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)

        # 5.4, 在todo 界面上显示创建和修改事件, 格式如下(24小时制)
        # 11/02 10:31
        create_time = time.strftime('%m/%d %H:%M:%S',
                                    time.localtime(t.created_time))
        update_time = time.strftime('%m/%d %H:%M:%S',
                                    time.localtime(t.updated_time))

        s = '<h3>{} : {} | start:{} | update:{} | {} |{}</h3>'.format(
            t.id, t.title, create_time, update_time, edit_link, delete_link)
        todos.append(s)
    todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')