Django is a high-level Python framework that follows the model-template-views architectural pattern. Django is one of the most popular web frameworks due to its security, scalability, and flexible design. Companies like Instagram, Reddit, and Dropbox all used Django.
- A basic understanding of Django
- Python3 installed on your machine
- Django installed on your machine
Let’s first create a project. In your command line, find the correct directory, and run the following command:
$ django-admin startproject django_login
This creates the project we’ll be working in. From there, we’ll need to create an application. Since we’re building a login screen, navigate into the project with
cd django_login, and run the following command:
$ python3 manage.py startapp login
Let’s now run the server to test if everything has been set up correctly. Run the following command:
$ python3 manage.py runserver
By deploying our Django application, we can check in real-time if the build is compiling correctly. Since we have only initialized our Django application, in localhost:8000/, you should see the following webpage:
Below is a command-line screenshot of setting up a Django application from scratch.
First, we’ll notify our project that we’ve created an application. To do this, go into the
django_login folder and find the
settings.py file. Scroll down to the
INSTALLED_APPS section and add
'login' (our application name) to the list of applications.
From there, we’ll need to edit the
urls.py file to account for our new application. We will be using the built-in
LoginView from Django which will display the login form and process the login action.
Replace the template code with the following:
from django.contrib import admin from django.urls import path, include from django.contrib.auth.views import LoginView urlpatterns = [ path('', LoginView.as_view()), path('login/', include('login.urls')), path('admin/', admin.site.urls), ]
First, within the
login folder, we’ll need to create a
templates folder, and then a
registration folder within. From there, we’ll create a
Because we’re using the built-in
LoginView of Django, we must provide the HTML template with the
login.html, we’ll add the following Bootstrap code (delivered via CDN for convenience, though there are other Bootstrap installation methods):