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