Introduction to Django

Introduction to Django

Welcome to this new journey!!! In this post, we will see the world of Django, one of the most popular web frameworks for Python developers. Django offers a robust toolkit and a wealth of features to help you build powerful web applications with ease.

In this we'll cover everything you need to know to get started with Django installation.

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It's designed to help developers build web applications quickly by providing reusable components, such as authentication, URL routing, database models, and templating. Django follows the Model-View-Template (MVT) architectural pattern, which is similar to Model-View-Controller (MVC) but with some differences in terminology and implementation.

Django is a Python web framework, thus requires Python to be installed on your machine. After installing open the command prompt and check the Python version by executing python --version.

Setting Up Django Development Environment on Windows

Step 1: Check Pip Version

Before we start, let's verify that Pip is installed on your system. Open Command Prompt or PowerShell and type the following command:

pip --version

If Pip is not installed, you'll need to install it. pip installation guide for detailed instructions.

Step 2: Install virtualenvwrapper-win

Now, let's install virtualenvwrapper-win, which will help us manage our virtual environments more easily. Run the following command:

pip install virtualenvwrapper-win

This will install virtualenvwrapper-win along with its dependencies.

Step 3: Create a Virtual Environment

With virtualenvwrapper-win installed, let's create a new virtual environment. Virtual environments are isolated environments for our projects, keeping dependencies neat and tidy. Run the following command:

mkvirtualenv test

This command will create a new virtual environment named "test" and activate it automatically.

Step 4: Install Django

With our virtual environment set up, let's install Django. Run the following command:

pip install django

This command installs the latest version of Django within our virtual environment.

Step 5: Verify Django Installation

After installing Django, let's ensure it's installed correctly. Execute:

django-admin --version

This command should display the version of Django installed in our virtual environment.

Step 6: Create a Django Project

We're almost ready to start our Django project. Execute the following command:

django-admin startproject my_projects

This command creates a new Django project named "my_projects" in your current directory.

Step 7: Run the Development Server

The command python manage.py runserver is used to start the Django development server. It's typically executed from within the directory of a Django project, where manage.py resides. This command launches a lightweight web server that serves your Django project locally for development and testing purposes.

Here's how you use it:

  1. Open your command prompt or terminal.

  2. Navigate to the directory of your Django project using the cd command. For example:

     cd C:\Users\Pooja\my_projects\project_name
    
  3. Once you're in the directory containing manage.py, run the runserver command:

     python manage.py runserver
    
  4. After running the command, you should see output similar to the following:

     Starting development server at http://127.0.0.1
     Quit the server with CTRL-BREAK.
    
  5. Once the development server is running, you can access your Django project by opening a web browser and navigating to the URL http://127.0.0.1 or http://localhost:8000/.

  6. To stop the server, you can press Ctrl + C in the command prompt or terminal where the server is running.

Running python manage.pyrun server is essential for local development as it provides a convenient way to preview and test your Django application before deploying it to a production environment.