views:

252

answers:

2

i am raising exception using

if UserId == '' and Password == '':
    raise Exception.MyException , "wrong userId or password" 

but i want print the error message on same page

class MyException(Exception):
    def __init__(self,msg):
        Exception.__init__(self,msg)
+1  A: 

You are not using the Users API? Assuming you are handling a POST request, how about this:

class LoginError(Exception):
    CODES = { 'mismatch': 'Wrong credentials', 'disabled': 'Account disabled' }
    ...

try:
    // your authentication code
    raise LoginError('mismatch')
    ...
    raise LoginError('disabled')
except LoginError as e:
    self.redirect(your_login_url + '?err=' + e)

# In login page you must not print arbitrary GET parameter directly
err_reason = LoginError.CODES[self.request.get('err')]`

(Login request should be using POST method because it changes the server's state, and it's good habit to redirect after a POST, thus a redirect.)

jholster
but it is not printing error message on the page
Rahul99
Just pass the err_reason variable to your template (updated the example code).
jholster
yes it worksthanks
Rahul99
A: 

Why raising an exception instead of just stop function execution and redirect to new page using return statement

Ilian Iliev