How to Set Up a Django Development Environment

How to Set Up a Django Development Environment

A Step-by-Step Guide to Installing and Configuring Django

Introduction

Django is a popular web framework written in Python. It enables the rapid development of secure and scalable websites. The first step to working with Django is setting up a Django development environment. This involves downloading and configuring the software and tools needed to create and test Django projects.

Prerequisite

  • Basic understanding of Python or Any Programming Language

  • Understanding of How Websites Work

Install Python

Django is a Python-based framework. Therefore, to use Django, you need to have Python installed on your computer. To install Python:

Python logo

Python Official Website: Focused on Download dropdown

  • Select the right OS - for Linux/Unix OS, select ‘source code’

  • Download the latest stable release

Python Release Page for macOS: Focused on Latest Stable Release

  • Open the downloaded package

  • Follow installation instructions

Python Installation Start Page

Setup Success Page

  • Close setup window.

Create a Virtual Environment

Isolating a project’s environment is good practice.

For instance, imagine having to work on 2 projects that require different versions of Django and other dependencies. Installing every required package globally on your computer would be a bad idea because, then, you would have to deal with the conflicts. Instead, you should isolate each project in a virtual environment, and download their dependencies in their respective environments.

So to create a virtual environment for this project, go to your command line and do the following:

  • Create a directory for this project (call it my_python_project or anything you like).

      mkdir my_python_project
    
  • Change your current directory to the project you just created.

      cd my_python_project
    
  • Create a virtual environment named env in the current directory.

      # macOS / Linux
      python3 -m venv env
    
      # Windows
      python -m venv env
    
  • Activate the virtual environment.

      # macOS / Linux
      source env/bin/activate
    
      # Windows
      env\Scripts\activate
    

Install Django

Having created and activated a virtual environment, the next step is to install Django right there in your project folder (which is now an active virtual environment). To install Django, you will need pipa tool for managing and installing Python packages. Don’t worry, pip is automatically installed in Python virtual environments.

  • Install the latest version of Django

      pip  install django
    

Start a Django Project

The next step is to begin a Django project. This is a folder that houses all applications and configurations that make a Django website. Simply put, it is your Django development environment.

  • Start a Django Project (call it my_project or anything you like).

      django-admin startproject my_project
    
  • Change your current directory to your new Django project.

      cd my_project
    

Run the Development Server

Django comes with a lightweight web server that allows you to simulate serving your project and applications. This works with a file named manage.py. You should find it in your current directory.

  • Optional: Check for a manage.py file in your django project folder.

      # macOS / Linux
      ls | grep manage.py
    
      # Windows
      dir | Search-String manage.py
    
  • Run the development server

      python manage.py runserver
    

In your browser, go to: http://127.0.0.1:8000/. You should see something like this:

Django Development Server: On Initial Launch

Voila! Your Django development server is now primed and ready to soar to new heights.

Conclusion

Setting up a Django development environment is where every Django project begins. First, you have to ensure that Python is installed on your computer. Then, you want to create a virtual environment to enjoy the benefits of isolated development. In that environment, you create a Django project which is where you’ve got everything you need to start building your next (or first) website with Django.

You could create applications, make configurations, and serve your project with the Django development server. Best of code luck🧩!

References

  1. Python.org. (2023). Download Python | Python.org. Python Software Foundation. Retrieved from Python.org.

  2. Python.org. (2023). PEP 405 – Python Virtual Environments. Python Software Foundation. Retrieved from PEP 405.

  3. Dataquest. (2023). A Complete Guide to Python Virtual Environments. Dataquest Labs, Inc. Retrieved from Dataquest Blog.

  4. Django. (2023). The web framework for perfectionists with deadlines. Django Software Foundation. Retrieved from Django.

  1. Mozilla Developer Network. (2023). Django Web Framework (Python). Mozilla. Retrieved from MDN Web Docs.