In this blog, I will explain and illustrate with pictures/commands the most effective ways to create a Django project from scratch.
Source- Django Documentation
First things first, because of the various dependencies in Python version and OS restrictions, I feel that the best way to start a Django project is using the virtual environment. Some of the problems that I faced while installing Django are
- pip is not supported in the latest macOS instead we need to use pip3
- New macOS has inbuilt Python version 2.7.18 and if we try to install any later versions there are a lot of changes to be made in the path(.zshrc file for macOS)
Anaconda is also an option to solve the python versioning problem but again we have to install Anaconda for that purpose. Thus, creating a virtual environment to start the project is the best way to it.
💡 I am using Python version 3.8.9 with Django version 2.2.26.
- Set up the virtual environment
python3 -m venv django-env
- Activate the virtual environment:
- Install Django in the virtual environment:
python -m pip install django
This will create the root directory with the same name as the project name with manage.py file that is required for running all further commands while working models and Django server. It will also create another folder with the project name that is the actual Python package for your project which contains the init.py, settings.py, urls.py, asgi.py, wsgi.py.
💡 You can check the project structure by going to /Users//django-env
Alright then, lets check if the project works on localhost. To do the same run the following command-
python manage.py runserver
If you have completed the above steps, you can run the following command to start an application which will contain the models, templates folder and views for building the webapp.
python manage.py startapp myapp
This will create a directory structure as shown below-
myapp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
💡 All apps that are created will be listed under the root directory of the Django project
You can check the entire project on GitHub or start building your application on top of it directly.