In this tutorial, we’ll learn how to install Django, create a new Django project and view its directory structure.
pip3 install Djangopython -m django --versiondjango-adminpip3 install --upgrade djangoTo create a Django project, we follow these steps:
django-admin startproject PROJECT_NAME to create a number of starter files for our Django project.cd PROJECT_NAME to navigate into your new project’s directory.For example
$ 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 Visual Studio Code.
Below I share how to install and use the 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
After installing tree, run tree to see the directory:
.
├── manage.py
└── mysite
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
manage.py file executes commands on our terminal. We won’t have to edit it, but we’ll use it often.mysite directory is the actual Python package for your project.__init.py__ is an empty file that tells Python that this is a Python package.settings.py file contains important configuration settings for our new project.urls.py contains directions for where users should be routed after navigating to a certain URL.asgi.py is an entry-point for ASGI-compatible web servers to serve your project.wsgi.py is 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 http://127.0.0.1:8000/ or http://localhost:8000/
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 rerun the server.
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 start the server on port 5000:
$ python manage.py runserver 5000
Django is based on Model-View-Template (MTV) structure. It has 3 parts.