Please Note: The following steps are for MacOS, however, in case there’s a difference, Windows commands are also provided.
1- Create a Project Folder
2- Open the folder using your Integrated Development Environment (IDE)
3- Open the terminal and make sure you’re in the project directory. You can check this using the command “pwd” (“echo %CD%” in Windows) and if you need to change the directory, you can use the command “cd” followed by the path to your intended directory.
4- Create a virtual environment
In the command above, “python3” is the command-line interface to the Python programming language, “-m” is a switch that tells Python to run a module as a script, and “venv” is the name of the module that creates virtual environments. “env” is the name of the virtual environment that you are creating. You can name it according to your specifications.
Virtual environments help keep the project isolated, clean, reproducible, and version controlled. Isolation ensures that packages and dependencies of different projects do not interfere with each other. Cleanliness ensures that the Python Installation on your system is free of dependencies specific to a particular project. Reproducibility means that the project environment can be easily reproduced, even if the dependencies or their versions have changed, making collaboration easier. Version control makes it easier to keep track of the dependencies required for a project.
Therefore, creating a virtual environment makes it easier to maintain and deploy the Django project.
5- Activate the virtual environment for the project
For Windows: “env\Scripts\activate”
As a side note, you can jump out of the virtual environment by using the command “deactivate”
6- Install Django on the virtual environment
7- Start the Django Project
The above command is used to create a new Django project. You can add a “.” after the project name to not create a new folder and create the project in the same folder.
“django-admin” is the command-line utility that comes with Django for administrative tasks. The “startproject” subcommand is used to create a new Django project. “API_med” is the name of the project to be created. When you run this command, Django will create a new directory with the name “API_med”, and this directory will contain the basic files and directories needed to get started with the Django project- as shown below:
8- Create a new Django App
Please Note: Make sure to be in the directory with “manage.py” file. You can check that by inserting the command “ls” (“dir” for windows) and you can change the directory using the commands explained in step 3.
In the above command “python” is the command-line interface to the Python programming language. “manage.py” is a command-line utility that comes with Django for administrative tasks. The “startapp” subcommand is used to create a new Django application within an existing Django project. “API” is the name of the Django application that we are creating. When you run this command, Django will create a new directory with the name “API” and this directory will contain the basic files and directories needed to get started with a Django application, as shown below:
9- Install your app in the “INSTALLED_APPS” section in the ‘settings.py’ file
Please note if you want to customise the configuration of the app you can use the following syntax and you can customise the behaviour in apps.py file:
This command basically specifies the path to the APIConfig class which is created as a subclass of the AppConfig class when we create an app. The APIConfig class inherits the methods from the AppConfig class, which we can overwrite, according to our specifications, using the Polymorphism principle of the Object Oriented Programming. For more information about Object Oriented Programming, you can check out my publication at https://medium.com/@jawad751/object-oriented-programming-and-family-3d9723e17d64
In conclusion, by following the steps outlined in this article, you can get started with building your own Django projects and creating powerful web applications. Whether you’re a beginner or an experienced developer, Django provides a robust and flexible platform for building high-quality web applications. So, take your time, follow the steps, and start building your next great project today!
PS Code for the above article can be found on GitHub at: https://github.com/JD751/how_to_create_a_new_project_on_django/tree/master