python - Redirect if failure in django views -


i have function. want execute following 2 lines:

list = utils.get_connection().get_list_by_id(mailchimp_list_id) list.subscribe(email_address, {'email': email_address, 'fname': fname, 'lname': lname}) 

and if fail, want redirect them '/mailing_list_failure'

here full function:

def add_email_to_mailing_list(request):     if request.post['email'] , len(request.post['email']) > 4:         email_address = request.post['email']         fname = request.post['fname']         lname = request.post['lname']         #if 2 next lines failure return '/mailing_list_failure/'         list = utils.get_connection().get_list_by_id(mailchimp_list_id)         list.subscribe(email_address, {'email': email_address, 'fname': fname, 'lname': lname})          return httpresponseredirect('/mailing_list_success/')     else:         return httpresponseredirect('/mailing_list_failure/') 

it sounds want use try / except block. not python expert, let me know if can improved, , i'll update it.

def add_email_to_mailing_list(request):     if request.post['email'] , len(request.post['email']) > 4:         email_address = request.post['email']         fname = request.post['fname']         lname = request.post['lname']         #if 2 next lines failure return '/mailing_list_failure/'         try:             list = utils.get_connection().get_list_by_id(mailchimp_list_id)             list.subscribe(email_address, {'email': email_address, 'fname': fname, 'lname': lname})             return httpresponseredirect('/mailing_list_success/')         except:             return httpresponseredirect('/mailing_list_failure/') 

check out link:

http://docs.python.org/2/tutorial/errors.html


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -