hello ML
Computer Science and programming articles. We do not sell courses.
Machine Learning
Data Structures
Algorithms
Problem Solving
C Programming
Python Programming
Web Dev
In this article, we’ll learn how to configure the login/logout functionality with Django’s built-in user authentication module. Django provides built-in functionality for user authentication using usernames. Now, let’s set up the project and get started without any delay.
For those of you who like reading the official documentation, you can check this out.
We’ll start by creating a new Django project. We can do all of the configurations from the command line and if we are using editors like VS Code, Pycharm, etc. can do the configuration using their terminals or using git-bash.
Python should be installed in the system and an editor (VS Code, Pycharm, Sublime Text, Atom, or any other editor with which you are comfortable) should be available.
Create a new directory for our code on the desktop, in our example, we’ll name it “project”.
Make a virtual environment for our project (completely optional but considered as good practice to use a virtual environment). You can refer to https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ for additional information on how to build your virtual environment.
Start the virtual environment.
Install django.
Create a new Django project. We’ll call it Login in our example.
Create an app. We’ll call it account in our example.
Migrate the changes to the SQLite database.
Run the development server.
After executing the runserver command, the following link ( http://127.0.0.1:8000 ) is created. Once you click on it, the webpage shown below will be opened. This shows that your installation is working successfully.
Django automatically installs certain apps while setting up the project, the auth app is one of them. Navigate to the file Login/settings.py and under INSTALLED_APPS you can see that auth is one of several built-in apps Django has installed. Here, we need to add the app we just created – account . There are multiple ways to add our apps here, but for now, let’s go with the simplest way.
Let’s make a superuser account from the command line. Quit the server by pressing ctrl+c in the terminal and then execute the command python manage.py createsuperuser . Answer any questions asked.
Note that the password will not appear on the screen for security reasons.
Using a superuser account, we can access the admin panel of our project by navigating to http://127.0.0.1:8000/admin/ .
To use the account app we need to add it to our project’s urls.py file. Make sure to add the include module from django.urls .
Also, create a python file named urls.py in the accoun t folder and add the following code.
We are creating a homepage that will display a heading “homepage” and one message to logged out users and another to logged-in users.
The touch command is used to create files if it does not exist. The file base.html contains the basic structure which would be used in all HTML files. The file base.html is used to reduce the redundant code and make HTML in the other files in the project more clean and easy to read. The file home.html contains the layout of our home page.
Now lets the following code to the file base.html .
We have to extend base.html in every HTML file where we want to use is using the {% extend ” “%} tag. Now fill the file templates/account/home.html with the code shown below.
Note that the templates should be created in a project-level folder , not in other directories like Login .
Now add the code shown below to project/templates/account/login.html .
Now add the code shown below to the signup.html form at templates/account.
Note: {% csrf_token %} tags are used for security concerns, namely to prevent a CSRF Attack. If the token is not present then the following error will be displayed:
Another error that you will get is TemplateDoesNotExist at /account/login.html.
Request Method:
GET
Request URL:
http://127.0.0.1:8000/
Django Version:
3.2.5
Exception Type:
TemplateDoesNotExist
Exception Value:
account/login.html
This error occurs when the developer doesn’t register templates in the settings.py file. We must tell the system to look got the templates in the folder called templates. To do this, update the settings.py file to tell Django to look for a templates folder at the project level.
The error will now be resolved. You also have to create URLs and views for all the pages (home, login, signup). Add the following code in account/urls.py .
Copy this code to account/views.py .
Startup the Django server again with python manage.py runserver and navigate to http://127.0.0.1:8000/ you’ll see the following.
If you pay attention, you’ll notice that we have just set up the HTML as of now. We still haven’t handled the data given in these forms. Now, let’s handle and set up the signup and login data. To perform this, add this code in the form action part in login.html and signup.html respectively.
In templates/account/signup.html , replace the form tag (in line 12) with the below shown code.
And in templates/account/login.html , replace the form tag (in line 7) with the code shown below.
Now, update the URLs in account/urls.py to point to the appropriate links.
Error will be displayed in the terminal because views corresponding to the URLs haven’t been made yet. Now, update views for URLs in views.py in account/views.py file.
Now, our project is ready for testing both login and logout functionality. Users can register themselves by signup, login into the home page, and log out from the home page.
By using the superuser account you created earlier, you can view all the registered users at http://127.0.0.1:8000/admin/auth/user/ .
If you need any help, try checking out this code on GitHub at https://github.com/hellomlorg/django-login-logou t.
Hope you enjoyed this article. In the coming articles, we will try to cover some other aspects of Django. For more such amazing articles check out hello ML . If you would like to improve this article or report something incorrect, please do let us know in the comments below.
If you have any copyright infringement claims, kindly send a mail to [email protected] . We deal with plagiarism very strictly and if our authors are found to be involved in plagiarism, we will take appropriate action against them after reviewing the claim.
Click to share on LinkedIn (Opens in new window)
Click to share on Twitter (Opens in new window)
Click to share on WhatsApp (Opens in new window)
Click to share on Telegram (Opens in new window)
Click to share on Facebook (Opens in new window)
Click to share on Reddit (Opens in new window)
Internship Guidelines at hello ML
Privacy Policy