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

发布于 2024-10-04 14:31:49 字数 797 浏览 0 评论 0原文

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 上的特定网址。

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')

My requirement is:
I will get a login and welcome page on same url. Login page has only one password field.

My problem:
If I get login and go to welcome page again on refreshing, it send me to login page. But ideally it should be on the welcome page only.

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

My second problem: I want to redirect on a particular url on 404.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深缘浅 2024-10-11 14:31:50

如果用户转到“/home”页面,您不想在向他们显示登录屏幕之前检查他们是否已登录吗?如果 HTTP 方法不是 POST,您似乎认为他们尚未登录。

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

If the user goes to the "/home" page, don't you want to check to see if they're logged in before showing them the login screen? It appears like you assume they're not already logged in if the HTTP method is not POST.

I don't know much about your framework, but I assume if the login is successful you should set a cookie, and then you should check the cookie for HTTP GETs to see if the user is authenticated.

安人多梦 2024-10-11 14:31:50

您的第二个问题已在此处得到解答。

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

Your second question is answered here.

You might also want to take a look at Beaker's cookie sessions and use that to keep your application's state between requests.

守望孤独 2024-10-11 14:31:50

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

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

If I understand the question correctly, the only way to get to render the welcome template is via POST.

You could change this so that GET requests check whether someone is logged in. If that fails, then redirect them to the login page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文