Creating Your First Django Project

Now that you have Django installed, it's time to create your first Django project. In this section, we'll guide you through the process of setting up a new Django project, step by step. By the end, you'll have a working project that you can build on as you learn more about Django.

Step 1: Set Up a Virtual Environment (If Not Done Already)

Before starting your Django project, it’s a good practice to use a virtual environment. If you haven’t set one up yet, follow the steps from the previous section to create and activate a virtual environment.

Step 2: Create a Django Project

To create a new Django project, you’ll use the django-admin command-line tool. This tool is included with Django and provides several utilities for project management.

  1. Navigate to the Directory: Open your terminal or command prompt and navigate to the directory where you want to create your Django project.

  2. Run the Startproject Command: Use the startproject command to create a new Django project. For example, to create a project named myproject, you would run:

    django-admin startproject myproject
    

    This command creates a new directory named myproject containing the basic structure of a Django project.

Step 3: Understand the Project Structure

Let’s take a closer look at the files and directories that Django has created for you:

  • myproject/: The outer directory is your project’s root directory. It contains the manage.py script and another directory with the same name as your project.

  • manage.py: This is a command-line utility that lets you interact with your Django project in various ways. You’ll use this script for tasks such as running the development server, creating apps, and more.

  • myproject/ (inner directory): This inner directory is the actual Python package for your project. It contains the following files:

    • __init__.py: An empty file that tells Python that this directory should be treated as a package.
    • settings.py: This file contains all the configuration settings for your Django project, such as database configuration, installed apps, middleware, and more.
    • urls.py: This file is where you’ll define the URL patterns for your project. It maps URLs to views.
    • wsgi.py: This file is used for deploying your project to a production environment using WSGI (Web Server Gateway Interface).
    • asgi.py: Similar to wsgi.py, but used for deploying your project using ASGI (Asynchronous Server Gateway Interface), which supports asynchronous web protocols.

    Image: Project structure with explanations for each file and directory.

Step 4: Running the Development Server

With your project created, you can now run the Django development server. The development server is a lightweight web server that comes with Django. It’s designed to help you test your project locally without needing a full-fledged web server like Apache or Nginx.

  1. Navigate to the Project Directory: Use the terminal to navigate into your project directory:

    cd myproject
    
    
  2. Run the Development Server: Use the manage.py script to start the development server:

    python manage.py runserver
    
    

    When you run this command, Django starts the server on http://127.0.0.1:8000/ by default. Open this URL in your web browser, and you should see the Django welcome page, which confirms that everything is set up correctly.

Step 5: Customizing the Server Port (Optional)

By default, the development server runs on port 8000, but you can change this if needed. For example, to run the server on port 8080, use the following command:

python manage.py runserver 8080

Similarly, you can specify the IP address and port if you want to make the server accessible from other devices on your network:

python manage.py runserver 0.0.0.0:8000

Next Steps

Now that your Django project is up and running, you’re ready to start building web applications. The next steps typically involve creating apps within your project, defining models, and setting up views and templates.