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

StackOverflow https://stackoverflow.com/questions/4289115

  •  28-09-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top