我正在瓶子里写一个小应用程序。我有一些概念性问题请帮助我解决
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果用户转到“/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.
您的第二个问题已在此处得到解答。
您可能还想查看 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.
如果我正确理解这个问题,渲染欢迎模板的唯一方法是通过 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.