In this lesson, you will learn how to create your first Django project. A Django project contains the main configuration and components required to build a web application.
A Django project is a collection of configuration files and applications that work together to build a web application.
The project contains important settings such as URLs, installed apps, database configuration and other project-level options.
Before creating a project, open your terminal and make sure the correct virtual environment is active.
For example:
venv\Scripts\activate
Verify that Django is available before creating the project.
python -m django --version
If a version number is displayed, Django is available in the current Python environment.
Django provides the django-admin command for creating projects.
django-admin startproject myproject
This command creates a new Django project named myproject.
The command contains two important parts:
django-admin startproject myproject
django-admin – Django command-line utilitystartproject – command for creating a projectmyproject – name of the projectYou can also create a Django project using Python's module execution syntax.
python -m django startproject myproject
This is useful because it explicitly runs Django using the current Python environment.
You can choose a meaningful name for your project.
For example:
django-admin startproject schoolproject
The project name should be a valid Python package name.
It is common to create the project inside a dedicated directory.
mkdir MyDjangoProject
cd MyDjangoProject
django-admin startproject myproject
Django will create the project structure inside the current directory.
After creating the project, you will see a structure similar to:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
manage.py is a command-line utility created automatically
when you create a Django project.
It is commonly used to run development commands for your project.
The settings.py file contains project configuration.
It can contain settings for databases, installed applications, middleware, templates, static files and other Django options.
The urls.py file is used to define URL patterns for the project.
It helps Django determine which view should handle a particular URL.
The __init__.py file marks the project directory as a
Python package.
It is created automatically as part of the project structure.
asgi.py provides an entry point for servers supporting
ASGI applications.
It is useful when deploying Django applications in environments that support asynchronous server interfaces.
wsgi.py provides an entry point for WSGI-compatible servers.
It is commonly relevant when deploying Django applications with traditional synchronous web server setups.
After creating the project, you can start Django's development server using:
python manage.py runserver
Django will start a local development server.
After starting the development server, open the local server address shown in the terminal.
Typically, Django uses:
http://127.0.0.1:8000/
You can open this address in a web browser.
To stop the development server, return to the terminal and press:
Ctrl + C
This stops the running development server.
Run manage.py commands from the directory containing
manage.py.
python manage.py runserver
This makes it easier for Django to locate the project's configuration.
Django can also create the project files directly in the current directory by using a dot:
django-admin startproject myproject .
The dot represents the current directory.
| Command | Result |
|---|---|
django-admin startproject myproject |
Creates a project directory named myproject |
django-admin startproject myproject . |
Creates project files in the current directory |
When creating a project, understand where you are running the command. Using the dot syntax can be useful when you already have a dedicated project directory.
For example:
mkdir MyDjangoProject
cd MyDjangoProject
django-admin startproject myproject .
After creating the project, check that the important files are present.
Project names can be customized according to the application.
For example:
django-admin startproject studentportal
This creates a Django project named studentportal.
A Django project and a Django app are different concepts.
The project contains overall configuration, while apps are used to organize specific functionality.
Apps will be covered in later lessons.
A common mistake is running the command from an unexpected directory.
Before creating a project, use cd to move to the directory
where you want your Django project to be created.
mkdir MyDjangoProject
cd MyDjangoProject
python -m venv venv
venv\Scripts\activate
python -m pip install Django
django-admin startproject myproject .
python manage.py runserver
This creates a project directory, prepares a virtual environment, installs Django, creates the project and starts the development server.
mkdir MyDjangoProject
cd MyDjangoProject
python3 -m venv venv
source venv/bin/activate
python -m pip install Django
django-admin startproject myproject .
python manage.py runserver
After successfully running the startproject command,
you have created the basic structure of your first Django project.
The next step is to understand what each project file and folder does.
The main command for creating a Django project is:
django-admin startproject myproject
You can also create the project in the current directory:
django-admin startproject myproject .
After creation, use manage.py to run Django commands such as:
python manage.py runserver
You now have the basic structure required to start building a Django application.
django-admin startproject creates a Django project.manage.py is used to run project management commands.settings.py contains project configuration.urls.py contains URL configuration.asgi.py and wsgi.py provide server entry points.django-admin startproject myproject . creates the project in the current directory.Question: Which command is used to create a new Django project?