How to Initiate the Development Environment when building a Django App
Tags: webdev, django
Published: 2023-11-13 10:15:56

## Introduction

Django is a versatile web development framework. Many top companies, including Instagram, FireFox and Spotify use the framework to develop their websites. Django is useful when developing complex, large-scale web apps. However, many developers use it to create simple web apps. This tutorial guides new Django developers on how to set up a Django development environment to create their web apps. The tutorial will guide on how to set up a Django app in both Windows and Linux environments.

**Prerequisites**: Ensure you have a Python version 3.4+ installed on your system.

Setting up a Django application involves two major steps:

1. Setting up a [Python virtual environment](https://www.geeksforgeeks.org/python-virtual-environment/)
    
2. Starting a Django project

[Jump to Windows set-up](#windows)
[Jump to Linux set-up](#linux)
    

<a id="windows"></a>
## Windows set-up

This set-up assumes you are running Windows 10

Follow these steps for Windows set up:

1. Create a project folder in your windows file system.

    For example, create the folder named **django_app** in your **desktop directory**

2. Start a Windows terminal
 1. Press `Win + R` to open a dialog box
 2. Type `cmd` then press `enter`

3. Navigate to the project folder
    ```cd Desktop\django_app```

4. Create a virtual environment
    Run `python -m venv my_env`
    **Note**: **my_env** is a custom name you give to your virtual environment

5. Activate the virtual environment
    Run `.\my_env\Scripts\activate`
    
6. Install Django using pip
    ```pip install django```
    
7. Start a Django project
    Run `django-admin startproject django_app .`
    **Note**: **django_app** is a custom name you give to your project. 
    Do not forget the dot at the end of the command or you may run into some configurations problems when deploying your app.
    
8. Change into the Django project directory
    ```cd django_app```
    
9. Create your Django application
    Run `python manage.py startapp my_first_app`
    **Note**: **my_first_app** is a custom name of the specific Django application you are creating
    
10. Add your application in the project's **settings.py** file
 1. Locate a file named settings.py within your project folder.
     **Hint**: You can locate it manually within your project folder. It should be in a subdirectory with a similar name to your project.
 2. Open the settings.py file using a code editor
 3. Locate a section called **INSTALLED_APPS**
 4. Add the name of your Django application at the end
 5. Save and close the settings.py file

11. Create your app's database schema
 1. Return to your terminal
 2. Run `python manage.py migrate`
     
    **Note**: It is necessary to run this operation to create a database that the app can work with
    
12. Start a development server
    In your terminal, with the virtual environment still active, run `python manage.py runserver`
**Note**: The operation will start a local development server on port 8000
    
13. View your project in a browser
Open a browser and enter the URL **http://127.0.0.1:8000/**
    

<a id="linux"></a>
## Linux set-up

This set up assumes you are running an ubuntu version 20.04+

1. Open an ubuntu shell on your system
    
2. Create a project directory and switch into the directory
    ```mkdir django-project && cd django-project```
    
3. Create a virtual environment
    ```python3 -m venv my_env```
    
4. Activate the virtual environment
    ```source my_env/bin/activate```
    
5. Install Django using pip
    ```pip install django```
    To install a specific Django version
    ```pip install django==3.2.21```

6. Start a Django project
    ```django-admin startproject django_project```
    
7. Switch into the Django project directory
    ```cd django_project```
    
8. Start a Django application
    ```python3 manage.py startapp django_app```
    
9. Add the new application to your settings.py file
 1. Locate a file, settings.py within your shell
 2. Open the file using a code editor
 3. Locate a section named **INSTALLED_APPS**
 4. Add `'django_app'`at the end of installed apps list
 5. Save and close the settings.py file

10. Create a database
    ```python3 manage.py migrate```
    
11. Start a development server on port 8000
    ```python3 manage.py runserver```
    
12. View your project in a browser
    Run **http://127.0.0.1:8000/**
    

## Conclusion

By following either steps depending on your operating system, you will have created a basic Django web application. Note that this is only a basic application, without any features. You need to build on the basic application by customizing it further to suit your project's needs or client's specifications