#100DaysOfWeb in Python Transcripts
Chapter: Days 53-56: Django part 2 - registration and login
Lecture: Django SMTP configuration and testing out email

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here I'm at the Django's email documentation
0:04 and Python overall makes it relatively easy
0:06 to send email, thanks to the SMTP module.
0:11 And I'm going to the SMTP backend section
0:14 we see that it's actually the default backend
0:17 and it turns out that defining these constants
0:22 in settings.py is basically all we need
0:25 to get SendGrid running.
0:28 My first attempt was Gmail, but I ran into
0:31 some security issues that Gmail was not very easy about
0:36 sending automated emails.
0:38 Way easier with SendGrid, so I'm using that now
0:41 for all my Django apps.
0:44 So we want to go to our app
0:47 and go to settings.py.
0:52 But actually first, I want to deactivate
0:55 my virtual environment and set my user and password
1:00 in environments and variables
1:03 because they should never be checked into version control.
1:08 So I'm going to the end and I'm going
1:10 to export SendGrid username.
1:18 And that's the username I used when I signed up
1:20 in the previous video.
1:24 And I'm going to set a SendGrid password.
1:29 Uppercase. That's, of course, a fake password.
1:38 So if you follow along
1:39 export those to environment variables
1:42 to feature our own username and password.
1:45 Then I'm going to use my script
1:48 which is a sort of deactivate script;
1:51 I'm using that so often I made an ads.
1:55 And now my environment variables should be active
1:58 in my virtual environment.
2:01 I go back to settings.py
2:04 so again, that's under mysite/settings.py
2:09 all the Django configuration is there.
2:12 I go towards the end.
2:13 I'm now going to set up the configuration variables
2:16 which we saw in the documentation
2:18 we don't need all of those.
2:20 We're going to use five of them
2:22 and then the email configuration will be available
2:26 when Django is launched and the registration plugin
2:32 will use that to send emails.
2:36 So first of all, we need an EMAIL_HOST
2:44 which is SMTPsendgrid.net.
2:51 We need an EMAIL_PORT which is 587.
2:59 We need to use TLS.
3:02 Not going too much into email and protocols
3:04 but we need TLS.
3:07 And then I need my EMAIL_HOST_USER.
3:14 And that's in your virtual environment.
3:16 And as we've seen before with the secret key and debug
3:19 those variables can be retrieved at os.10 environ.
3:24 I'm not going to use the get method
3:27 I really want the program to exit when this key
3:31 is not available because it's a requirement.
3:38 And lastly, we want an EMAIL_HOST_PASSWORD.
3:43 And that's the sendgrid underscore password redefined.
3:49 Just to show how it would look if such
3:52 an environment variable was not there
3:54 I can make a typo
3:59 Django and it complains, so that's good.
4:03 Of course, that was a deliberate mistake.
4:05 So this is all there is to it
4:07 an email hos, port and tls protocol
4:11 and the user and password I defined
4:12 when making my SendGrid account.
4:16 Let's test it out. So that worked.
4:21 And now let's sign up to my app.
4:41 Wow, nice, it didn't crash.
4:44 You've now registered, activation email is sent.
4:47 So let's retrieve that email, which should have come
4:50 with an activation link.
5:05 And my PyBites blog user account was linked to our tml
5:08 so I'm going to retrieve it there.
5:27 And here I've got an email with an activation link; perfect.
5:32 So I can copy that over.
5:37 Thanks for signing up, your account is now activated.
5:40 Enjoy. Great.
5:42 That's actually my template text.
5:44 I have yet to define a login link
5:46 that's in the next video
5:48 so I'm just going to hit the login path manually now.
6:02 And here's the redirect we defined earlier
6:06 but I made a mistake there, so let's fix that.
6:16 And I actually forgot to name space this
6:19 so I cannot just say quote list
6:21 because in quotes in the app
6:24 in the urls.py, I set an app name
6:28 and this effectively is a name space
6:30 so if I reference these views
6:34 quote_list quote_detail, etc.
6:36 I have to prepend them with the quotes name space
6:40 to avoid any conflict.
6:41 So in another app, I would have another quote_list
6:45 for some reason, it's not likely
6:46 then I make sure that this one is bound to this app.
6:54 So let's quickly fix that. Try this again.
7:07 Awesome, now it works and I got logged in.
7:11 Of course we should be able to log in and log out
7:13 from the header and that's what the next video is about
7:17 to add that here, to the navigation bar.