Files
awesome-cheatsheets/backend/django.md
2022-11-16 13:20:58 +03:00

1.9 KiB

What is Django

  The Django framework implements the Model-View-Template architectural pattern,
  or MVT for short, which is actually a modification of the MVC (Model-View-Controller) pattern common in web programming.

The main elements of the pattern:

  •   URL dispatcher: When a request is received, based on the requested URL,
      determines which resource should handle this request.
  •   View: receives a request, processes it, and sends some response back to the user.
      If processing a request requires access to the model and database, then the View interacts with them.
      Can use Template or templates to create a response.
      In the MVC architecture, controllers (but not views) correspond to this component.\
  •   Model: describes the data used in the application.
      Individual classes generally correspond to tables in a database.\
  •   Template: Represents the presentation logic as generated html markup.
      In MVC, this component corresponds to View, that is, views.

Installing and configuring Django

Pip package manager

  If pip was previously installed, you can update it with the command

  •   pip install --upgrade pip

Installing a virtual environment (venv)

  Create a virtual environment

  •   python -m venv .venv

Activating a virtual environment

  Activation in Windows on the command line

  •   .venv\Scripts\activate.bat

  Activation on Linux and MacOS

  •   source .venv/bin/activate

Installing Django

  Installing Django

  •   python -m pip install Django

  If we are interested in a specific version of Django, then we can specify it during installation:

  •   python -m pip install django~=4.0.0

Deactivate a virtual environment

  After finishing working with the virtual environment, we can deactivate it using the command:

  •   deactivate