DEV Community

Cover image for Introduction to Django and Setting Up the Development Environment
kihuni
kihuni

Posted on

Introduction to Django and Setting Up the Development Environment

In the previous post, we introduced Python and covered its basics. Now, let's dive into Django, a powerful web framework for building robust backend applications. We'll also guide you through setting up your development environment for Python and Django, ensuring you're ready to start coding.

Introduction to Django

What is Django?

Django is a high-level Python web framework that enables the rapid development of secure and maintainable websites. It follows the "batteries-included" philosophy, providing everything you need to build a web application out of the box. Django is known for its scalability, reusability, and the ability to handle high-traffic sites.

Key Features of Django

  • Django follows the Model-View-Template (MVT) architecture, which is a variation of the MVC pattern that encourages a clear separation of concerns and enhances code organization.
  • Automatic Admin Interface: Django comes with a built-in admin interface, allowing for easy management of application data without the need for additional code.
  • ORM: Django's Object-Relational Mapping (ORM) enables interaction with the database using Python code rather than SQL queries.
  • Security: Django includes built-in protection against common security threats such as SQL injection, cross-site scripting (XSS), and cross-site request forgery (CSRF).
  • Scalability: Django is designed to handle high traffic and large datasets, making it suitable for both small projects and large-scale applications.

Setting Up Your Development Environment

Before you start building applications with Django, you need to set up your development environment. Follow these steps to get everything ready:

Step 1: Install Python

Make sure you have Python installed on your system. Django requires Python 3.7 or higher. You can download the latest version of Python from the official Python website.

Step 2: Install Virtual Environment

Using a virtual environment is a good practice as it allows you to manage dependencies for different projects separately. Python's venv module helps you create virtual environments easily.

# Create a virtual environment
python -m venv myenv

# Activate the virtual environment
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate

Enter fullscreen mode Exit fullscreen mode

screen shot of terminal

Step 3: Install Django

Once your virtual environment is activated, you can install Django using pip, Python's package installer.

pip install django

Enter fullscreen mode Exit fullscreen mode

Show installation of django

Step 4: Create a Django Project

With Django installed, you can create a new Django project using the django-admin command.

# Create a new Django project named 'myproject'
django-admin startproject myproject

Enter fullscreen mode Exit fullscreen mode

Screenshot showing setting up django project

This command creates a directory named myproject with the following structure:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py

Enter fullscreen mode Exit fullscreen mode
  • manage.py: A command-line utility that helps manage the Django project.
  • myproject/: The main project directory.
  • settings.py: Contains all the project settings.
  • urls.py: The URL declarations for the project.
  • wsgi.py and asgi.py: Entry points for WSGI/ASGI-compatible web servers.

Step 5: Run the Development Server

Navigate to the project directory and start the development server to see your new Django project in action.

cd myproject
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

screenshots running running server

Open your web browser and go to http://127.0.0.1:8000/. You should see the Django welcome page, confirming that your development environment is set up correctly.

Image description

Step 6: Create a Django App

In Django, projects are composed of apps. Each app is a module that encapsulates specific functionality. To create a new app, use the startapp command.

# Create a new app named 'blog'
python manage.py startapp blog

Enter fullscreen mode Exit fullscreen mode

screenshot shows creating app in django

This command creates a directory named blog with the following structure:

blog/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    migrations/

Enter fullscreen mode Exit fullscreen mode
  • admin.py: Configuration for the Django admin interface.
  • apps.py: App-specific configuration.
  • models.py: Defines the data models for the app.
  • tests.py: Contains tests for the app.
  • views.py: Handles the logic for the app's views.
  • migrations/: Stores database migrations.

Step 7: Configure the App

To use the new app in your project, you need to add it to the INSTALLED_APPS list in the settings.py file.

# myproject/settings.py

INSTALLED_APPS = [
    ...
    'blog',
]

Enter fullscreen mode Exit fullscreen mode

Next Steps

With your development environment set up and a basic Django project and app created, you're ready to start building your backend application. In the next part of this series, we'll explore Django's ORM, views, templates, and more, as we dive deeper into backend engineering with Django.

Stay tuned for more!

Top comments (0)