我正在用瓶子写一个小应用程序。我有一些概念上的问题帮助我解决它

时间:2010-11-26 23:10:22

标签: python bottle

from bottle import  route, run, debug, error, request, template

@route('/home')
@route('/home/')
def login():
    return template('templates/login')

@route('/home', method='POST')
@route('/home/', method='POST')
def welocme():
    data = request.POST
    if data:
        password = data.get('password')
        check_pass = 'password'
        if password == check_pass:
            return template('templates/welcome')
        else:
            return template('templates/login')
    else:
        return template('templates/login')

我的要求是: 我将在同一个网址上获得登录和欢迎页面。登录页面只有一个密码字段。

我的问题: 如果我登录并在刷新时再次访问欢迎页面,则会将我发送到登录页面。但理想情况下,它应该只在欢迎页面上。

@error(404)
def error404(error):
    return 'http://google.com'

我的第二个问题:我想重定向404上的特定网址。

3 个答案:

答案 0 :(得分:1)

如果用户进入“/ home”页面,您是否要在显示登录屏幕之前检查他们是否已登录?看起来你假设如果HTTP方法不是POST,他们还没有登录。

我对您的框架了解不多,但我认为如果登录成功,您应该设置一个cookie,然后您应该检查HTTP GET以查看用户是否经过身份验证。

答案 1 :(得分:1)

您的第二个问题已经回答here

您可能还想查看Beaker's cookie sessions并使用它来保持应用程序在请求之间的状态。

答案 2 :(得分:0)

如果我正确理解了这个问题,那么渲染欢迎模板的唯一方法就是通过POST。

您可以对此进行更改,以便GET请求检查是否有人登录。如果失败,则将其重定向到登录页面。