In this Python tutorial, we will explore How to create a Django project. Moreover, we will understand the Django project, its file structure, and its use case. In addition, we will explore launching and stopping a Django development server.
Create Project in Django
The website application that is deployed with the help of the Django framework is simply called a “Django Project”. The following is the step-by-step procedure to create a project in Django.
Prerequisites to create Django project
Before start, make sure of the following prerequisites installed on your system:
- Python: Python is a high-level, interpreted programming language. Make sure it is installed with version 3.6 or higher according to your project requirements.
- Python Package Manager (Pip): Pip is a command line package manager tool that simplifies the process of installing, managing, and distributing Python libraries and modules.
Installing Django
Before moving forward, first, we should install Python and Django in our system. And for this, you can refer to the following article “How to install Django“.
Now, the steps for setting up your first Django project will be the same for all the operating systems. We can use different tools or utilities like visual studio code, pycharm, command prompt, etc. But the commands and steps for them will also be pretty much the same.
For this tutorial, we will be using the command prompt in Windows 10 for demonstration.
After installing Django, before setting up your first project in Django, we need to understand How we can create a project in Django.
Create Django Project
Let’s first understand what is a project in Django.
A project in Django can be referred to as a whole web application with its all parts. It can also be referred to as a collection of settings and apps for a specific web application. A Django project can contain multiple apps that have different functionalities. And we can also use these apps in different projects.
Let’s understand this with a real-life example, and the example is as follows.
I want to build an e-shop using Django. So, the e-shop will be my Django project. Now, the project will have some functionality like register new users, add new products to the e-shop, etc. For this, we will create Django apps within our e-shop project.
Now, let’s see how to create our first Django project. For this, open the command prompt and move to the location where you want to create your project. And now, run the following command.
django-admin startproject project_name
Let’s use this command to create our first Django project.
This will create a new directory with the name myProject in our current location. And this new directory will also contain some important Python files which have their own importance.
Understand Python Django Project Structure
Django organizes a project into multiple directories and files. Here is the directory structure.
Here, myProject/ is a root directory that contains manage.py file and another directory with the same name myProject.
Now, the name of this root directory is not much important, we can also change this name. But the other directory with the name myProject within the root directory is very important as it is a Python package. And we have to use this name to import anything inside it.
Now let’s understand the importance of these automatically created files in the project directory.
- manage.py– It is a command-line utility that is used to interact with your Django project.
- __init__.py– It is an empty file that indicates Python to consider this directory as a python package.
- settings.py– This file contains all the settings and configuration of your Django project.
- urls.py– This file is used to create and manage URLs related to your web application.
- asgi.py– It is used for ASGI configuration for your Django project.
- wsgi.py– It is used for WSGI configuration for your Django project.
So, with this, we have successfully created our first project in Django.
Running Development Server in Django Project
After successfully creating the project now it’s time to understand how we can run the Django project.
When we install Django, it comes configured with a lightweight server. And if we want to develop a web application locally in our system then, this lightweight server is efficient for this task.
Note- It is not recommended to use this lightweight server in production. So, always configure the Django project with the server that you want to use in production.
Now to run our project, we need to start the server, for this, we will use the manage.py utility. So, move to the project root directory and use the following command.
python manage.py runserver
This will return an output similar to this.
As you can see in the output, there are no issues with our project and the development server is been started. Now, we can access our web application at the following URL “http://127.0.0.1:8000/“. And when we open this URL in our browser, it will return a congratulatory message. Here is how it will look.
So, with this, we have successfully started our first Django project.
Stopping of Django Development Server in Project
Once we have started our project, the development server will be automatically reloaded when we make some requests. It will also reload when we make some changes in our Python code. There are very few situations when we need to restart the development server.
Now, to restart our development server, we should know, How to stop a running development server. For this, we just need to enter “Ctrl+C” and it will stop the server immediately.
Conclusion
We have successfully learned the procedure of creating the Django project from the beginning. We have understood the complete information about the Django project and the file structure of the Django project.
In addition, we have explored the functionality of running and stopping of Django development server with a practical example.
You may also like reading the following articles.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.