DEV Community

Cover image for Most Efficient Way to Create a Django Project and Django App
shlokabhalgat
shlokabhalgat

Posted on

Most Efficient Way to Create a Django Project and Django App

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

  1. pip is not supported in the latest macOS instead we need to use pip3
  2. 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.

Steps to create a Django project:

  • Set up the virtual environment
python3 -m venv django-env
Enter fullscreen mode Exit fullscreen mode
  • Activate the virtual environment:
source django-env/bin/activate
Enter fullscreen mode Exit fullscreen mode
  • Install Django in the virtual environment:
python -m pip install django
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Steps to create a Django App

python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

💡 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.

Top comments (0)