Getting Started With Django
Getting Started With Django
net/django-tutorial/getting-started-with-django/
components:
Web Browser
Django allows you to rapidly develop web applications with less code by taking advantage of its
framework.
Django follows the DRY (don’t repeat yourself) principle, which allows you to maximize the code Database
reusability.
• The web browser requests a page by a URL and the web server passes the HTTP request to
Django uses the MVT (Model-View-Template) pattern, which is slightly similar to MVC (Model-
Django.
View-Controller) pattern.
• Django matches the URL with URL patterns to find the first match.
The MVT pattern consists of three main components:
• Django calls the view that corresponds to the matched URL.
• Model – defines the data or contains the logic that interacts with the data in the database.
• The view uses a model to retrieve data from the database.
• View – communicates with the database via model and transfers data to the template for
• The model returns data to the view.
representing the data.
• The view renders a template and returns it as an HTTP response.
• Template – defines the template for displaying the data in the web browser.
The Django framework itself acts as a controller. The Django framework uses URL patterns that Creating a virtual environment
send the request to an appropriate view.
A virtual environment creates an isolated environment that consists of an independent set of
In practice, you’ll often work with models, templates, views, and URLs in the Django application. Python packages.
By using virtual environments, you can have projects that use different versions of Django. Also,
Django architecture
when you move the project to a different server, you can install all the dependent packages of
The following picture shows how Django manages HTTP request/response cycle using its the project using a single pip command.
First, create a new directory django-playground : It’ll show something like this:
Second, navigate to the django-playground directory: Note that you likely see a higher version.
Third, you can create a requirements.txt file using the pip command:
cd django-playground
This command creates the requirements.txt that includes all dependent packages of the
python -m venv venv
project.
Fourth, activate the virtual environment: When you move the project to a new server e.g., a test or production server, you can install all
the dependencies using the following pip command:
venv\scripts\activate
pip install -r requirements.txt
Note that you can deactivate the virtual environment using the deactivate command: administrative tasks such as creating a new project and executing the Django development
server.
deactivate
To run the Django, you execute the following command to list all Django core commands:
First, issue the following pip command to install the Django package: Output:
pip install django Type 'django-admin help <subcommand>' for help on a specific subcommand.
sendtestemail
The django_project is a Python package that consists of the following files:
shell
showmigrations • __init__.py – is an empty file indicating that the django_project directory is a package.
sqlflush • settings.py – contains the project settings such as installed applications, database
sqlmigrate
connections, and template directories.
sqlsequencereset
• urls.py – stores a list of routes that map URLs to views.
squashmigrations
startapp • wsgi.py – contains the configurations that run the project as a web server gateway
For now, we’re interested in the startproject command that creates a new Django project. The Running the Django development server
following startproject command creates a new project called django_project :
Django comes with a built-in web server that allows you quickly run your Django project for
development purposes.
django-admin startproject django_project
The Django development web server will continuously check for code changes and reloads the
This command creates a django_project directory. Let’s explore the project structure. project automatically. However, you still need to restart the web server manually in some cases
such as adding new files to the project.
cd django_project
To run the Django development server, you use the runserver command:
The urls.py contains a default route that maps /admin path with the admin.site.urls view:
python manage.py runserver
Once the server is up and running, you can open the web app using the URL listed in the output.
It’ll show a login page:
Typically, the URL is something like this:
http://127.0.0.1:8000/
Now, you can copy and paste the URL to a web browser. It should show the following webpage:
To stop the Django development server, you open the terminal and press the Ctrl-C (or
Command-C) twice.
Summary
• Django is a Python web framework that allows you to rapidly develop web applications.
• Django uses the MVT (Model-View-Template) pattern, which is similar to MVC (Model-View-
Controller) pattern.
• Use the python manage.py runserver command to run the project using the Django
development web server.
• Press Ctrl-C (or Cmd-C) to stop the Django development web server.
9 of 9 1/12/2023, 11:16 AM