Before we dive into Django, it is worth starting with the basics of web development and the Python programming language.
Web development involves building websites and web applications that users interact with through a browser. The front end is typically built with HTML, CSS, and JavaScript, while the back end handles the data and the logic. Python can be used for both, though it is most at home on the back end.
Django is a high-level Python web framework for building web applications quickly. It provides a lot of built-in functionality for the tasks that come up in almost every project, such as user authentication and database management.
Setting up your environment
Here is how to do it on a Linux machine. First, check that Python 3 is installed:
python3 --versionIf it is not there, install it with your system's package manager. On Ubuntu, along with pip, the package installer for Python:
sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pipIt is good practice to use a separate virtual environment for each Django project. A virtual environment is an isolated Python environment, so packages you install for one project do not affect the global environment or any other project.
python3 -m venv myenv
source myenv/bin/activateWith the environment active, install Django into it:
pip install djangoCreating your first project
Navigate to the directory where you want the project to live, then create it:
django-admin startproject myprojectTo check that it works, move into the project directory and start the development server:
python manage.py runserverOpen http://localhost:8000 in your browser and you should see the Welcome to Django page.
How a Django app fits together
A Django application is made up of four kinds of piece, and almost everything you write falls into one of them:
- Models define the structure of the data your application stores in a database.
- Views handle the logic for processing requests and returning responses.
- Templates define the HTML markup returned to the user.
- URLs map incoming URLs to specific views.
Building your first app
From inside your project directory, create the app:
python manage.py startapp myappDefine a model. In your myapp directory, create models.py:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=50)
description = models.TextField()That defines a table in your database with two columns, name and description.
Create and apply a migration. The first command writes a migration file based on the changes you made to your models. The second applies it and creates the table:
python manage.py makemigrations
python manage.py migrateCreate a view. In myapp, create views.py:
from django.shortcuts import render
from .models import MyModel
def index(request):
mymodels = MyModel.objects.all()
return render(request, 'myapp/index.html', {'mymodels': mymodels})This view gets all instances of MyModel from the database and hands them to a template.
Create a template. Inside myapp, create a templates directory, and in it a file called index.html:
{% for mymodel in mymodels %}
<h2>{{ mymodel.name }}</h2>
<p>{{ mymodel.description }}</p>
{% endfor %}Define a URL. In myapp, create urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]That maps the root URL of the app to the index view.
Wire it into the project. Finally, include the app's URLs in your project's urls.py, which serves the app under /myapp/:
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')),
]That is a working Django app. There is a great deal more to learn, including user authentication, forms, and deploying to a production server, but the shape of every Django project is the one you have just built: models, views, templates, and URLs.





