Setting Up Django from Scratch: A Beginner’s Guide

Dec. 19, 2024



If you’re starting your journey in web development and have decided to use Django, you’ve made an excellent choice. Django is a powerful, Python-based web framework that simplifies the development process and helps you create robust, scalable applications. This guide will walk you through setting up Django from scratch.


What You’ll Need

Before diving into Django, make sure you have the following prerequisites:

  1. Python Installed: Django is built on Python, so you’ll need Python 3.7 or later installed on your system.
  2. A Virtual Environment: This ensures your Django project has its own isolated space, free from conflicts with other Python projects.
  3. Pip: The package manager for Python to install Django and other dependencies.

Step 1: Install Python

If Python isn’t installed on your machine:

  • Windows: Download and install Python from python.org. During installation, ensure you check the box to add Python to your PATH.
  • Mac/Linux: Python typically comes pre-installed. If not, use your package manager (brew for macOS or apt for Linux) to install it.

Verify the installation by running:

bash
python --version

Step 2: Create a Virtual Environment

A virtual environment helps keep your Django project dependencies separate. To create one:

  1. Open your terminal and navigate to the directory where you want to set up your project.

  2. Run the following command to create a virtual environment:

    bash
    python -m venv myenv

    Replace myenv with a name for your environment.

  3. Activate the virtual environment:

    • Windows:
      bash
      myenv\Scripts\activate
    • Mac/Linux:
      bash
      source myenv/bin/activate

Once activated, your terminal will show the virtual environment’s name, e.g., (myenv).


Step 3: Install Django

With the virtual environment activated, install Django using pip:

bash
pip install django

You can verify the installation by checking the Django version:

bash
django-admin --version

Step 4: Start a New Django Project

  1. Run the following command to create a new project:

    bash
    django-admin startproject myproject

    Replace myproject with the name of your project.

  2. Navigate into your project directory:

    bash
    cd myproject

Step 5: Run the Development Server

To ensure everything is set up correctly, run the Django development server:

bash
python manage.py runserver

Open a browser and go to http://127.0.0.1:8000/. You should see Django’s default welcome page, confirming your setup is complete.


Step 6: Create a Django App

Django projects are composed of one or more apps. To create your first app:

  1. Run:

    bash
    python manage.py startapp myapp

    Replace myapp with the name of your app.

  2. Register the app in your project by adding it to the INSTALLED_APPS list in myproject/settings.py:

    python
    INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', # Add your app here ]

Step 7: Database Setup

Django comes pre-configured with SQLite, but you can connect to other databases like PostgreSQL or MySQL. For now, let’s use SQLite:

  1. Apply migrations to set up the default database schema:

    bash
    python manage.py migrate
  2. Create a superuser to access the admin panel:

    bash
    python manage.py createsuperuser

    Follow the prompts to set a username, email, and password.

  3. Run the development server again and visit http://127.0.0.1:8000/admin/ to access the admin panel.


Step 8: Build Your First View

Let’s create a simple view:

  1. Open myapp/views.py and add:

    python
    from django.http import HttpResponse def home(request): return HttpResponse("Hello, Django!")
  2. Map the view to a URL. In myapp/urls.py (create this file if it doesn’t exist), add:

    python
    from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]
  3. Link your app’s URLs to the project. In myproject/urls.py, add:

    python
    from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')), # Include your app’s URLs ]

Visit http://127.0.0.1:8000/, and you’ll see “Hello, Django!” displayed in your browser.


Conclusion

Congratulations! You’ve set up Django from scratch and created your first view. From here, you can start building more complex applications by exploring Django’s features like templates, models, and forms. Django’s extensive documentation and active community make it easy to learn and grow as a developer.

Ready to build something amazing? Start experimenting with Django today!