I have this function. I want to execute the following two 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 they fail, I want to redirect them to '/mailing_list_failure'
Here is the full function:
def add_email_to_mailing_list(request):
if request.POST['email'] and len(request.POST['email']) > 4:
email_address = request.POST['email']
fname = request.POST['fname']
lname = request.POST['lname']
#If two next lines are failure return to '/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 like you want to use a try
/ except
block. I am not a python expert, so let me know if this can be improved, and I'll update it.
def add_email_to_mailing_list(request):
if request.POST['email'] and len(request.POST['email']) > 4:
email_address = request.POST['email']
fname = request.POST['fname']
lname = request.POST['lname']
#If two next lines are failure return to '/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 this link:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.