Skip to content

Introduction

MTV (MVC)

Django is a free (BSD licensed) python framework for developing web applications. To implement the application, it uses the complex MTV design pattern (also known as MVC):

  • Model - data from the database
  • Template (View) - data presentation
  • View (Controller) - business logic

There are some differences in the interpretation of the template and logical components between MVC and MTV, but they are so insignificant that they are not worth paying attention to and it can be assumed that MTV is another MVC nomenclature.

Battery-included:

Django has many built-in components that help in the development of applications written in it

  • ORM
  • Template language
  • Configurations to describe database migrations
  • Support for authentication and authorization
  • Support for translations in different languages
  • Other

Most of these elements will be discussed in detail later in the course.

Let's get started!

The first thing to get started with Django is to prepare your local environment by installing Django.

python -m pip installdjango==3.0.10

Then, with the following command we can generate a Django directory tree for our project:

django-admin startproject hollymovies .

After running this command, we should see a directory tree similar to the one below:

startproject

django-admin is a script installed with Django, and startproject is the most popular of its options for generating a directory tree and basic project files. By putting the project name as another argument, django-admin will create a directory with the specified name and use it as the project name. By adding the . argument to the end, no additional nesting will be created. The command "with a dot at the end" should be used when we already have a version control system repository created, and without a dot - when we want to initialize it in the newly created directory.

The .gitignore file is not generated by the django-admin startproject command.

To start the application server, run the command pythonmanage.py runserver

runserver

manage.py is the django-admin generated script that is the entry point for our project. Each project has a separate manage.py script. runserver is its argument to start the server. This will be one of the options for running the manage.py script that we will explore.

After running python manage.py runserver and clicking on the generated link http://127.0.0.1:8000 we should see the following page in the browser:

runserver

This is a page automatically generated by Django. This page will only be available at the beginning before applications are added ...

ATTENTION!

The manage.py script is the entry point for our application. We'll be using it over and over again while learning and working with Django. runserver is just one way to run this script!

django-admin startproject creates a Django project. Django also uses the so-called application. Design is a broader concept than an application. The project collects one or more applications. To avoid any misunderstandings in communication, it is very important to correctly use and distinguish between the two concepts from the very beginning. Now let's add application:

python manage.pystartapp viewer

Calling the manage.py script above will generate a application folder in a project folder.

app

What's the difference between a project and a application?

  • The project collects one or more applications
  • Application is a possibly unitary set of functionalities independent of other applications
  • Example: by making a project of an on-line store, it could be divided into a application sales, application blog where specialists publish industry-related articles, and application contact the store, automatically sending emails with questions to employees
  • We often create single-application projects

For Django to be able to use the abstractions built into our application, let's add it to the "installed applications" in the file hollymovies/settings.py:

# Application definition

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'viewer'
]

In file hollymovies/viewer/views.py let's create a view function:

from django.http import HttpResponse


def hello(request):
  return HttpResponse('Hello, world!')

View is a normal feature. However, in order for Django to know that it needs to be run after reaching a certain address, it must be registered in the urls.py file:

from django.contrib import admin
from django.urls import path

from viewer.views import hello

urlpatterns = [
  path('admin/', admin.site.urls),
  path('hello', hello)
]

How it's working?

  • Browser sends request HTTP to a specific URL
  • URL dispatcher looks for a function registered in the file urls.py for a specific URL
  • Django runs the found function with the argument request containing the data from the browser turned into a Python object
  • The function returns a Python response object
  • Django delivers (converted to HTTP compatible) response to the browser

User data

User can provide data via URL in two ways:

  • Regular expression - URL is parsed against the described regular expression and values found in "variables" in angle brackets are entered as named string arguments to the view function
  • URL encoding - a method of describing dictionaries in URL addresses (the dictionary is encoded with a question mark, individual key-value pairs are separated with "&" characters, keys and values are separated with equal signs, and special characters are encoded by preceding a percent sign and entering the appropriate code ) forwarded to a HTTP request by GET

Let's look at the following value substitution examples cruel under the variable s.

Regular expressions

In the views.py file we register the function hello that takes an additional argument called s.

from django.http import HttpResponse


def hello(request, s):
  return HttpResponse(f'Hello, {s} world!')

In urls.py we register the address as a pattern with a variable s in angle brackets

urlpatterns = [
  path('admin/', admin.site.urls),
  path('hello/<s>', hello)
]

Let's check http://localhost:8000/hello/cruel. The end of the address is substituted for the variable s in the view.

URL encoding

In the view function, the value of the variable s coded in the address is taken from the object "HTTP methods" GET by referring to the key s.

from django.http import HttpResponse


def hello(request):
  s = request.GET.get('s', '')
  return HttpResponse(f'Hello, {s} world!')

Let's check http://localhost:8000/hello?s=cruel. The encoded dictionary appears in the request.GET object.