Creating Your First Project

Understanding Django Project Structure

When you start a new Django project, various files and directories are created. Here's a glance at the typical Django project structure:

python image
  • manage.py: It is a command-line utility that lets you interact with your Django project and is automatically created when starting a new Django project. You can use it to run the development server, make database migrations, and more. - project_name/: This directory contains the project's main settings and configuration files. - project_name/__init__.py: An empty file that tells Python this directory should be considered a package. - project_name/settings.py: Contains settings and configurations for the project, such as database settings, installed apps, middleware, and more. - project_name/urls.py: Defines the URL patterns for the project and maps them to views. - project_name/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. - project_name/asgi.py: An entry-point for ASGI-compatible web servers to serve your project (used for asynchronous functionality).

Creating a New Django Project

Ensure Django is installed to create a new Django project. View installation guidelines from here. (Link to installation)

Use the command below to create a project:

django-admin startproject myproject

This will create a new project directory named ‘myproject’ with a basic project structure. Now, change into your project directory using the command below:

cd myproject

Running the Development Server

Firstly, verify your Django project has a ‘manage.py’ file. Now, start your development server with the command below:

python manage.py runserver

HINT:

You can see warnings related to unapplied migrations. We’ll deal with that later in the database section.

To access the development server, open a web browser and navigate to http://127.0.0.1:8000/. You should see the Django welcome page, indicating that your development server is running successfully.

To stop the running development server, press Ctrl+C in your terminal interface.

HINT:

If you want to run the development server on any other port than the default (i.e., 8000), you can provide the port as: For instance, to use port 8005:

python manage.py runserver 8005