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


Talk Python's Mastodon Michael Kennedy's Mastodon