Intro and why authentication matters
Welcome back, aliens. Let's continue this series on Django. Now, till this point we were able to fetch detail from database and then you are able to display those destinations on the page, but then we are missing one big thing and that is the login and registration. Of course, when you build an application that application will be having a lot of users.
User roles and choosing where to place auth
Now some of the users will be admin and then we have worked with the admin user details as well, right? So we have created the admin. Now admin can add data to the website from the backend, of course, but what if you want to work with normal users? Yes, you can create accounts for them as well. So let's say for your application you have ten users, so what will you do? You will manually create accounts for them. Now that is possible and then you can give a login form to them; they can simply log in through your application. Now that is one way. The another way is you can have a registration form for users so that they can register themselves. Now that makes much more sense, right, to have a login and registration form for your application. But if you know that you will be having the limited predefined users, admin will add all the users. But here, let's create two forms: one for login, one for registration. Initially we'll start with registration.
Preparing the environment and creating the accounts app
There are certain things we have to change. First of all, since we have restarted the machine, we have to make sure that we are working in the environment. So if you go back here, first of all I have to activate work on test, and then we need to run the server because if you go back to the browser and press Enter you can see server is not running. So I have to run the server first. I will say python -m runserver and then this will run the server. Okay, so you can see we are in the server now. Let's go back here and we got our page; okay this is what we wanted. So what we need here is we want to say login and registration. Now for that, where will you put it? So of course right in your form there should be a way where you can keep your login and register some button. So now instead of changing this existing design, since we don't have a predefined form for registration, we'll change here itself. So let's change About Us, we don't want Services and News; we'll remove that and we'll change that with Login and Registration. You can also remove Contact if you want, but then if you want to have those features of course we'll be having two forms as well. Now as we have discussed in the previous sessions, whenever you want to create something different, for example we have to work with a calculator, now we are working with a traveler application, now if you want to have the user registrations — you know login, logout, registration — you can create a separate module for it. You have a choice: you can still continue with the same app, which makes much more sense while you're learning, but what also makes sense is to learn something new, right? Why do we get modules? Now you can have two different modules here, right? We have a travel module, we can have a traveler module, so that we can reuse the modules. Let's say if you want to use this module for some other application we should be able to do that. So let's create a separate module here and that will be... let me just click here and let's create a module for accounts. So how do we do it? It's very easy. We have done that before. So we can simply say python manage.py startapp and then we have to mention the name, which in this case I will say accounts. You can give any name, it doesn't matter. So we go for accounts. Oh okay, so we have to make sure that we are working in the environment so I missed that. Let me just do that quickly. Now since we are using a new command prompt, we have to say work on test and now let's again hit that same thing and now you can see we got the app named accounts. If you go back here there should be accounts, so you can see that we have accounts here and if I expand accounts it's a new app now.
Adding accounts URLs and initial view
That's why you can see we don't have init.py so we also need that. So first of all let's create it. So I will go back to the accounts and we need a urls.py because we will be working with all these links: login, registration, and logout. So let's do that here. Now I don't want to type it from scratch; we will be copying it from travel. So let's go back to travel's urls, let's copy this and go back to our urls and paste. This is the new urls.py we are creating. So what will change? The first thing is we have to say path. Let me just get that on a new line. Anyway the moment I say it will go back to its previous position. But let's change it now. The URL which we need here should not be blank because that's for the main application which is travel. So what will be the URL here? Now, first one we need three, right? So let's start with the registration and then we can add login and logout later. So it's this register and in the index we have to say register. Of course you can go with any name but let's go for registration and register so you can see we got our URL mapping for register. If someone is calling register we will be calling this. But you can see we got an error now; it's obvious, right? We are working for a long time now; we know what will be going wrong here. The urls.py now the view is views.py of the new app which we have done. So if you go back to views.py you can see we don't have anything here. So basically we have to get a function which will be named register. Okay so we will be doing that. But do you think it will work if even if I do that? Actually not. The moment you create a new app, if you remember, every time you get a new app and if you have your own urls.py you have to map it with the original urls.py and we have not done that. So let's do that quickly. Let's go back to the project's urls. This is the main URL mapping we have and we have to add one more here. So we'll give a comma, that's always there. So we'll say path and this time we have accounts. Now here we have to include the urls of accounts, right? So you have to say include and in brackets we have to say accounts.urls. The same thing we have done for travel, remember. But travel is the main module which is working with this slash or the home URL. But if you want to access accounts we'll be using accounts slash. Okay, so whatever URL like login, logout will be accounts slash login, accounts slash logout. That makes sense. We have done this part as well just to finish things quickly. But now it's important to work with views.py. So basically what we have done is we have created this new app which is accounts and then we have mapped it so whenever a request comes it will come to the urls.py. So we can say path register and we have to accept a request. Okay and then we can't keep a function with pass — basically if you create a function but it's not doing anything. Now if you want to make it work of course you need to call a URL and that will be coming from the index.html. This is where I want that button of registration. So basically we need three buttons: login, logout, and registration. As of now let's only talk about registration because that's what's important. So I will go back here and let me remove all this About and the Services and News. Let's have index.html and contact there and in between I want to have the registration or maybe you can do it afterwards. We'll see. Copy this and we'll paste. Now this is where instead of having contact.html we will be saying register, but register belongs to accounts now so we can't simply say register here. So we have to say accounts/register. Let's change this contact to register and our job is done. Let's verify this work. So let's go back to the home page and say refresh and you can see we got Home, Contact, and Register. The moment you click on this it will call the function which is a stub which is doing nothing. Can you see that we got an error? It says a view accounts.register didn't return an HTTP response. That's important, right? We can't simply say pass; that will not work. So we have to do some programming there.
So let's do it. Basically you have to go to views.py and here you have to call the page which is registration. So we'll return and we have to render a page by passing the request object first and the page name is register. So we'll say register.html. But now you will say we don't have this page and you're right we don't have this page so we have to create a register.html. So we'll do that in templates here and let's create register.html. Now we can have a very simple page here, nothing complex. Maybe in our template actually we don't have a registration HTML so that we can get a good view. Let's create a simple registration form. You can make an amazing UI after that, that's your choice, but let me make it simple. So I will say I want a basic HTML sample first of all, and in the body I want to create a form. So first of all let me say the title as destination. In the body we have to get a form and form action — now what will it call? I would say it will call register and since we are submitting data now we have to say method equal to post. So we got a form and then we have to find some fields. Now first of all we don't know how many fields were required here; maybe we need two fields, three fields, five fields. Now when you say you want to register the data, this registration will go to the server, right, and on the server side we have database. First of all we do have a database here so we need to check. Now when we talked about migration it has created a lot of tables in your database. One of the tables is for users. Let's see if that works. Let's go to PGAdmin just to verify if we have database available or table available. So when I open my PGAdmin if I go back to the database which that is, Postgres 11, in that we have the default base and you can see we have a table here which is auth_user. If I view all data we have used it when we were working with superuser and you can see we already have two users here; we got Kiran and Naveen, but both these users are actually superuser. You can see we are saying true and true. So basically they can login through the admin panel as well. But now we want to create a normal user, not superusers. So we have this table already and in this table you can see columns: id, password, last_login also something, so we need password, username, right? We also need first name, last name; in fact those are optional but let's use that. We have email; we will ask this from the user. The system will pick up the date joined. So the fields are password, username, first name, last name, and email. So basically we need five fields. Let's do that. Let's go back to our form and let me quickly use it. But since we are getting a form we have to remember one more thing: we have to use the CSRF token. We have talked about it before. So let's add the token. Now we have to create those fields. Let's do that quickly. So we've got input type text and then we want to have a name for it. Let's start with the first name. I will say name equals first_name. That's what the name in the database is, so first_name, and then we will have a placeholder and the placeholder will be First Name. This is something which will be shown in the text field. Of course you can also have a label here but just to keep it simple I'm going for First Name, and job is done. Let's have a br tag; I'm just trying to keep it as simple as possible. Copy. Now how many fields we need here? So basically we need five, right? So let's do that. We got five; in fact we need one more when you say password field: when you go to any website for registration we have two password fields. Password one is password and second is confirm password. We'll do that here as well. So we'll have six fields. The second one is the last name, next field will have username and here we'll have username, the next field is email id — in fact what we'll do for email let's keep type equal to email — and for the next one we'll say password or maybe I will say password1 because we have two passwords here, password1 and password2, and this is confirm password. Okay and then the type would be password. Done. We got our form here, right? But how will you verify this? Let's try. So I will save this code. In fact we have done all the coding required to get this form at least. Let's go back to our browser and say refresh and you can see we got our form and we also got placeholders: we got first name, last name, username, email, password and confirm password. The button is missing — can you believe that? Okay, so we need to also get a button there. We'll say input type is equal to submit, value Register, something button as well. Let's go back here, add the submit button. Now let's check if this is working from the home page. We want to call this page, press Enter, and you can see Register, click on Register, we got the form. But then this code will not work even if you submit the details here; it will not save the data in database because we have not done the actual coding as of now and that will be in the next video. So in this particular video we have created a special app which is for accounts and it will be doing login, registration and logout, and this time we have plated the page for registration so that a user can register itself. But this code will not work because the coding is still remaining. So that was it. See you in the next video. I hope you are enjoying the entire series. Comment section and do subscribe.