Easily Install Django 4.2: A Quick Guide
The goal of this article is to get you started using Django to build a web application as quickly as possible, and set you on the road to a production-ready application. For demonstration purposes, we’ll be creating a simple blog for our company called Foo.
Installation
Assuming you have Python setup with pip – installation is simple. This will install Django along with a useful command that will help you get a project started quickly.
pip install django
The following command creates the project directory foo/
and creates a basic project structure and some initial files.
django-admin startproject foo
Let’s quickly go over each file we created:
manage.py
is the command-line utility that Django provides. It comes with a number of commands that are supported out of the box, some of which we’ll see later. Some Django applications that you may install later on will add commands accessible through this utility, and ultimately you’ll be able to add your own as needed.
foo/settings.py
is an initial example of a Django settings file that should contain all the configuration for your application. If you take a look, you will see the default values that set your application up for running in a local development environment.
foo/urls.py
is the default location for your URL configuration. Essentially this provides Django with the rules of how to route requests your application receives.
foo/wsgi.py
is where the actual WSGI application lives. When running Django in production, you’ll want to use a server like uwsgi or green unicorn, whose servers interface with the application that resides in this file. Now that we’ve created the project, let’s take it for a spin. To run the built-in development server, run:
./manage.py runserver
You should see some output indicating the settings used and where the server is available. Note that by default, it makes the server available at http://127.0.0.1:8000. If you go to that address, a default page will load, letting you know that you have successfully installed Django. As the page indicates, the only reason you’re seeing that page is because the default configuration provided in the settings.py file has DEBUG = True
. Otherwise, trying to reach that page would give you a 404 as your URL configuration in urls.py doesn’t route that path to anything within your application.
Keeping track of dependencies
Before we move on, let’s create a requirements.txt file in the base directory of the project to keep track of any dependencies we install using pip. So far, we’ve only installed Django, so our requirements.txt file will look something like:
django==2.1.7
Creating a Django Application
The terminology gets a bit confusing, but a Django project is made up of a number of applications (not to be confused with the WSGI application). To start working on our blog, let’s create an application that will provide the blog functionality to our Django project:
./manage.py startapp blog
The startapp command, like startproject, creates a template application for you. Once again, let’s take a look at the files we created:
blog/__init__.py
is the standard python __init__.py to make the directory into a Python module.
blog/admin.py
is the file for registering your models with the Django Admin.
blog/apps.py
contains the Django Application configuration object. This tells Django details about your application.
blog/migrations/
is a folder for your database migrations. This is mostly managed by Django’s ORM. We will see how to make migrations and apply them later.
blog/models.py
is where you should put your models using the Django ORM. This represents the interface with your backend data store.
blog/tests.py
provides a place to put the tests for testing your application.
blog/views.py
provides definitions for your applications views. This is where our logic for handling requests go.
Registering Your Application
In order to let your Django project know about your application, you’ll need to register it by adding it to INSTALLED_APPS
in settings.py
. Your INSTALLED_APPS
should now look this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
Defining Models
Now that our application is registered, let’s define the data model for our blog. Let’s say we want our blog to show a list of posts that are tagged with various topics and that people who have registered with our blog have the ability to comment on the posts.
Back to Top