Summary: in this tutorial, you’ll learn how to create a new Django project, understand the project structure, and launch the Django web app from a web browser.
Django is a Python web framework that includes a set of components for solving common web development problems.
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 reusability.
Django uses the MVT (Model-View-Template) pattern, which is slightly similar to MVC (Model-View-Controller) pattern.
The MVT pattern consists of three main components:
- Model – defines the data or contains the logic that interacts with the data in the database.
- View – communicates with the database via model and transfers data to the template for representing the data.
- 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 send the request to an appropriate view.
In practice, you’ll often work with models, templates, views, and URLs in the Django application.
The following picture shows how Django manages HTTP request/response cycle using its components:
- The web browser requests a page by a URL and the web server passes the HTTP request to Django.
- Django matches the URL with URL patterns to find the first match.
- Django calls the view that corresponds to the matched URL.
- The view uses a model to retrieve data from the database.
- The model returns data to the view.
- The view renders a template and returns it as an HTTP response.
Creating a virtual environment
A virtual environment creates an isolated environment that consists of an independent set of Python packages.
By using virtual environments, you can have projects that use different versions of Django. Also, when you move the project to a different server, you can install all the dependent packages of the project using a single
First, create a new directory
mkdir django-playgroundCode language: plaintext (plaintext)
Second, navigate to the
cd django-playgroundCode language: plaintext (plaintext)
Third, create a new virtual environment using the
python -m venv venv
Fourth, activate the virtual environment:
The terminal will show the following:
Note that you can deactivate the virtual environment using the
Install the Django package
First, issue the following
pip command to install the Django package:
pip install django
Second, check the Django version:
python -m django --version
It’ll show something like this:
4.1.1Code language: CSS (css)
Note that you likely see a higher version.
Exploring Django commands
Django comes with a command-line utility program called
django-admin that manages administrative tasks such as creating a new project and executing the Django development server.
To run the Django, you execute the following command to list all Django core commands:
Type 'django-admin help <subcommand>' for help on a specific subcommand. Available subcommands: [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate optimizemigration runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserverCode language: plaintext (plaintext)
For now, we’re interested in the
startproject command that creates a new Django project. The following
startproject command creates a new project called
django-admin startproject django_project
This command creates a
django_project directory. Let’s explore the project structure.
The following shows the
├── django_project | ├── asgi.py | ├── settings.py | ├── urls.py | ├── wsgi.py | └── __init__.py └── manage.pyCode language: plaintext (plaintext)
Here’s a quick overview of each file in the Django project:
manage.pyis a command-line program that you use to interact with the project like starting a development server and making changes to the database.
django_project is a Python package that consists of the following files:
__init__.py– is an empty file indicating that the
django_projectdirectory is a package.
settings.py– contains the project settings such as installed applications, database connections, and template directories.
urls.py– stores a list of routes that map URLs to views.
wsgi.py– contains the configurations that run the project as a web server gateway interface (wsgi) application with WSGI-compatible web servers.
asgi.py– contains the configurations that run the project as an asynchronous web server gateway interface (AWSGI) application with AWSGI-compatible web servers.
Running the Django development server
Django comes with a built-in web server that allows you quickly run your Django project for development purposes.
The Django development web server will continuously check for code changes and reloads the project automatically. However, you still need to restart the web server manually in some cases such as adding new files to the project.
To run the Django development server, you use the
python manage.py runserverCode language: CSS (css)
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). ... Django version 4.1.1, using settings 'django_project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.Code language: plaintext (plaintext)
Once the server is up and running, you can open the web app using the URL listed in the output. Typically, the URL is something like this:
Now, you can copy and paste the URL to a web browser. It should show the following webpage:
urls.py contains a default route that maps
/admin path with the
from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]Code language: Python (python)
To open the admin page, you use the following URL:
It’ll show a login page:
Stop the Django development server
To stop the Django development server, you open the terminal and press the Ctrl-C (or Command-C) twice.
Create requirements.txt file
requirements.txt file contains all dependencies for a specific Django project. It also contains the dependencies of dependencies.
To create a
requirements.txt file, you run the following
pip freeze > requirements.txtCode language: plaintext (plaintext)
When you move the project to a new server e.g., a test or production server, you can install all the dependencies used by the current Django project using the following
pip install -r requirements.txtCode language: plaintext (plaintext)
Download the Django Project source code
- 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
django-admin startproject new_projectcommand to create a new project.
- Use the
python manage.py runservercommand to run the project using the Django development web server.
- Press Ctrl-C (or Cmd-C) to stop the Django development web server.