Python for Entrepreneurs Transcripts
Chapter: Building your email list
Lecture: Form to handle the sign up request

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Our next goal is going to be to make this form work.
0:06 Remember we put this form on the page
0:08 when we did our bootstrap stuff, but it went nowhere.
0:11 Well guess what, it's going to go somewhere now.
0:13 In fact, there is a few steps that I thought "well,
0:15 it's not really worth you watch me do this
0:17 because we've done it several times over and over again",
0:20 so I did a little bit of work off-camera, let me just give you the rundown on that
0:23 so I created a newsletter controller, and now if you look back here,
0:27 and you look where this form submits to,
0:30 you'll see it goes to newsletter/add_subscriber as a POST.
0:35 OK, so this is unusual because it's not posting back to the same page, right,
0:41 but it's kind of weird to post back to index, the homepage,
0:44 so we are posting over to this area and letting this
0:47 controller handle newsletters all on its own.
0:49 So here this is a post to newsletter/add_subscriber,
0:52 and we don't have the GET, basically the index is the GET,
0:55 so our first goal is going to be to get that information from the subscriber,
1:00 what they've posted to the form and then we're going to go and call,
1:04 once we have the email address, which we don't have yet, we're going to call,
1:08 we are going to go to the mailing list service and import that
1:13 and then we're going to call add_subscriber with email.
1:17 And we also maybe want to check whether or not this works here so we'll say
1:21 "if this did work" then what are we going to do?
1:25 We are going to redirect over to "hey, you subscribed",
1:31 if something else, if it doesn't work, we'd like to redirect to /newsletter/failed.
1:38 Now how do I know that? Because over here, I've actually created
1:42 well you can see the two methods down there but more importantly,
1:45 I've created the "subscribed" and "failed" pieces and these are more or less static HTML
1:49 that says "well, we had a problem subscribing you, failed",
1:52 or subscribe like "hey, you are on the list, awesome, thanks for signing up,
1:57 here is a few more things you can do."
1:59 So what we're going to do is we want to actually finish this method,
2:04 so that we can capture this,
2:07 and then we're going to write just a little bit of detail here
2:10 in this video and then we'll come back and do a proper MailChimp API section.
2:14 OK, so how do we get a hold of this email here? Well,
2:17 we could go and actually create a view model but it's literally one piece of data
2:22 so I am kind of thinking we're just going to get direct access to it
2:26 now if you look down here in the base controller,
2:28 I've created this property called data_dict,
2:31 which merges the GET / POST and matchdict so this is routing, this is query string,
2:36 and this is the form POST, OK. And it is like order precedence from bottom to top,
2:43 so I am just going to use this property
2:46 and somewhere in there is going to be the email presumably,
2:48 so we'll do something like this we'll say self.data_dict.get('email')
2:52 and let's just go back to verify that's actually what we're calling this, I think it is,
2:59 but go to our form, which is right here, name is email,
3:04 that's the one that is going to tell us what we've got to look for,
3:08 I just want to get that, we probably want to do a test,
3:10 although we could have this thing check as well,
3:13 so we could say "if not email... go like so".
3:17 Right, maybe we even want to check the format of the email
3:20 but the MailChimp API should tell us if we get it wrong.
3:23 We also have a "required", if you look back over here,
3:27 you can see we have a required and it's a type="email",
3:30 so at least form the HTML perspective this should be mostly good,
3:33 but of course, people could post to this end point without actually going through our HTML
3:37 or without running the validation, using some other mechanisms,
3:43 so we want to make sure we validated here as well.
3:46 So next let's go down here and return nothing, well,
3:49 this is actually going to return None, which is going to come back False,
3:52 and so it's going to go to "failed", but let's just run this
3:54 and see it's running through its steps here.
3:58 I am going to come down here, here is my email address,
4:01 and let's get notified, remember, it's going to call that function,
4:04 the function is going to return None implicitly, that's going to not be True,
4:07 so we should go to "failed".
4:10 And apparently, add subscriber is missing its email,
4:13 did I forget to pass that? Let's look.
4:15 Here. Oh no, I upgraded that from an instance method,
4:20 to a static method without realizing that PyCharm had helped me out there,
4:25 so no I did pass the email, I just screwed up.
4:28 Alright, let's run that again, add subscriber,
4:30 yes I want to send whatever that was,
4:32 boom, oops, it seems that we were wrong with that signup,
4:35 try hitting the back button, verifying your email address was valid,
4:38 so we could look and yes, it is valid, of course,
4:40 our code is broken that's why we're actually ending up here, right,
4:43 we haven't written it, but that's kind of flow,
4:45 alternatively, it should say "yeey, you succeeded,
4:47 welcome to the list and we are happy to have you part of our community."