In this tutorial, we’ll learn how to install Django, create a new Django project and view its directory structure.
pip3 install Django
python -m django --version
pip3 install --upgrade django
To create Django project, we follow these steps:
django-admin startproject PROJECT_NAMEto create a number of starter files for our Django project.
cd PROJECT_NAMEto navigate into your new project’s directory.
$ django-admin startproject mysite $ cd mysite
Note: You can’t name projects after built-in Python or Django components.
We can view the directory with the
tree command or with Visual Studio Code.
Below I share how to install and use tree package.
To install command line tools like the tree command on Mac, you can use the Homebrew package manager.
After installing Homebrew, run
brew install tree (Y)
$ sudo apt-get install tree
tree to see the directory:
. ├── manage.py └── mysite ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py
manage.pyfile executes commands on our terminal. We won’t have to edit it, but we’ll use it often.
mysitedirectory is the actual Python package for your project.
__init.py__is an empty file that tells python that this is a Python package.
settings.pyfile contains important configuration settings for our new project.
urls.pycontains directions for where users should be routed after navigating to a certain URL.
asgi.pyis an entry-point for ASGI-compatible web servers to serve your project.
wsgi.pyis how Python and webserver communicate.
We can start a development server by running
python manage.py runserver.
We’ll see this.
Django version 3.2, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
127.0.0.1 is localhost. This development server is being run locally on your machine, meaning other people cannot access your website.
This is a running webserver so we have to leave it running while you’re viewing your site in the browser or else you won’t be able to see it.
To view the site, we access
You’ll see a default site that Django has created for us and we’ll modify this.
To stop the server, type ctrl C.
If you have any trouble with the website not reloading for any reason, then stop the server and run the server again.
By default, the development server starts at port 8000.
To change the server’s port, we can pass it as a command-line argument. For example, we starts the server on port 5000:
$ python manage.py runserver 5000
Django is based on Model-View-Template (MTV) structure. It has 3 parts