EngineeringPython

Getting Started with Django

From an empty Linux machine to a working Django app: environment, project, models, views, templates, and URLs.

Kushan Manahara

March 12, 2023 · 3 min read

0735
Getting Started with Django

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:

terminal.sh
python3 --version

If it is not there, install it with your system's package manager. On Ubuntu, along with pip, the package installer for Python:

terminal.sh
sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pip

It 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.

terminal.sh
python3 -m venv myenv
source myenv/bin/activate

With the environment active, install Django into it:

terminal.sh
pip install django

Creating your first project

Navigate to the directory where you want the project to live, then create it:

terminal.sh
django-admin startproject myproject

To check that it works, move into the project directory and start the development server:

terminal.sh
python manage.py runserver

Open 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:

terminal.sh
python manage.py startapp myapp

Define a model. In your myapp directory, create models.py:

myapp/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:

terminal.sh
python manage.py makemigrations
python manage.py migrate

Create a view. In myapp, create views.py:

myapp/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:

myapp/templates/index.html
{% for mymodel in mymodels %}
<h2>{{ mymodel.name }}</h2>
<p>{{ mymodel.description }}</p>
{% endfor %}

Define a URL. In myapp, create urls.py:

myapp/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/:

myproject/urls.py
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.

Written by

Kushan Manahara

Responses (0)

Verified name, role, and email required before posting.

No responses yet

Be the first to share your thoughts, benchmarks, or feedback above.