当前位置: 动力学知识库 > 问答 > 编程问答 >

python - I am writing a small app in bottle. I have some conceptual problem help me to solve it out

问题描述:

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.

网友答案:

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.

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: